0

我有在 C#、Windows Phone 7、Silverlight 中执行此操作的示例代码,并且我正在尝试找到如何在 C++ Qt 中执行等效操作(我正在使用 Blackberry 10 Beta 3 SDK)。我可以使用QNetworkAccessManager吗?或者有更好的办法吗?如果可以的话,我将不胜感激有关如何执行此操作的一些帮助以及一些示例代码。这是示例 C# 代码:

var request = new RestRequest(url, Method.POST);

request.AddParamater("example", "example");
request.AddParamater("example2", "example2");

request.AddFile("file", fileData, filename, "image/pjpeg");

RestSharp.RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
   if (response.StatusCode == HttpStatusCode.OK)
   {
       //upload successful
   }
   else
   {
       //error occured durring upload
   }
});

这是我尝试过的东西:

// postVars is a QByteArray that contains a query string of the arguments 
// being passed in then I pass it in to my prepareFileUploadRequest file, 
// which appends the data for   uploading a file.

void Utils::prepareFileUploadRequest(QMap<QString, QString> *d,
        QNetworkRequest &request, QByteArray &postVars, QString path,
        QString scriptName, QString filename,
        QString stockNum)
    {

        request.setUrl(QUrl(Utils::ApiUrl));

        postVars.append("Content-type: multipart/form-data, ");
            postVars.append("boundary='---';\r\n\r\n");
        postVars.append("---\r\n");
        postVars.append("Content-Disposition: form-data; name='file' );
            postVars.append("filename='examplePic.jpg'\r\n");
        postVars.append("Content-Type: image/jpg\r\n");
        postVars.append("Content-Transfer-Encoding: binary\r\n\r\n");

        QFile file(path);
        if (!file.open(QIODevice::ReadOnly)) {
            return;
        }

        postVars.append(file.readAll() + "\r\n");
        postVars.append('---');

        request.
                setRawHeader(QString("Content-Type").toAscii(),                   
                QString("multipart/form-data; boundary='---'").toAscii());

            request.
                setRawHeader(QString("Content-Length").toAscii(),            
                QString::number(postVars.length()).toAscii());

            QNetworkAccessManager *nam = new QNetworkAccessManager(this);
            connect(nam, SIGNAL(finished(QNetworkReply *)), 
                    this, SLOT(handleUpload(QNetworkReply *)));
            nam->post(request, postVars);
 }

我在上面调用那个函数,然后我得到一个 xml 回复——它说我没有权限——而且它没有上传任何文件——所以这可能是使用的 php webservice 的问题,但它很难让我知道我是否采取了正确的方法,或者我是否做错了什么。}

4

1 回答 1

0
QString body = "" ;

const QUrl url("http://someurl.to/make/the/post" );
 QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
 QHttpPart textPartData;
 body = "" ;
 QTextStream(&body) << " this is your text to post with the file";
 textPartData.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=utf-8");
 textPartData.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
 textPartData.setBody(body.toAscii());

     for(int i=0; i< number_of_files_to_send ; i++){
             QHttpPart imagePart;
             QString header_body = "";
             QTextStream(&header_body) << "form-data; name=\"" << name_of_the_file << "\"; filename=\"" << name_of_the_file << ".jpg\"" ;
             imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(header_body));
             imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));

             //prepare file for upload
             QString filepath;
             QTextStream(&filepath) << FunkyPigeonBlackberry::getInstance()->dataModelProductDetails()->custom_image(i).value("cut_image").toString();

             // pack the new file
             QFile *file = new QFile(filepath);
             file->open(QIODevice::ReadOnly);
             imagePart.setBodyDevice(file);
             file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
             multiPart->append(imagePart);
     }

 multiPart->append(textPartData);
 multiPart->append(textPartProductDetails);
 multiPart->append(textPartDataDelivery);


 QNetworkRequest request(url);

 QNetworkReply* reply = m_networkAccessManager->post(request, multiPart);
 connect(reply, SIGNAL(finished()), this, SLOT(onNetworkResponse()));
于 2013-05-07T06:42:37.530 回答