在Qt5中上传Multipart表单数据中的文件

时间:2016-07-04 08:09:36

标签: qt file-upload multipartform-data

我正在拼命地将文件上传到Multipart中的服务器。我的代码与Qt文档几乎相同,但文件没有上传到服务器。

这是我在调试中的内容:

---------Uploaded-------------- 3672 of 3672

---------Uploaded-------------- 3672 of 3672 

---------Uploaded-------------- 3672 of 3672 

---------Uploaded-------------- 0 of 0 

----------Finished-------------- 

"Error transferring http://MyUrlHere.com/uploadFile - server replied: Bad Request" 400 QNetworkReplyHttpImpl(0x17589ff0)

问题不是来自服务器,因为当我尝试使用Chrome或Firefox扩展程序在多部件中上传文件时,它确实有效!

这是我的代码:

     QUrl testUrl("http://MyUrlHere.com/uploadFile ");
     QNetworkRequest request(testUrl);
     QNetworkProxy proxy;

     proxy.setType(QNetworkProxy::HttpProxy);
     proxy.setHostName("proxy");
     proxy.setPort(8080);
     QNetworkProxy::setApplicationProxy(proxy);

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

     QString preview_path  = "C:/Users/Desktop/image.jpg";
     QHttpPart previewPathPart;
     previewPathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_path\""));
     previewPathPart.setBody(preview_path.toLatin1());

     QString preview_name = "image.jpg";

     QHttpPart previewFilePart;
     previewFilePart.setHeader(QNetworkRequest::ContentTypeHeader,    QVariant("image/jpeg"));
     previewFilePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ preview_name + "\""));
     QFile *file = new QFile(preview_path);

     file->open(QIODevice::ReadOnly);
     previewFilePart.setBodyDevice(file);
     file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

     multiPart->append(previewPathPart);
     multiPart->append(previewFilePart);

     QNetworkAccessManager *networkManager= new QNetworkAccessManager;
     reply = networkManager->post(request, multiPart);
     multiPart->setParent(reply); // delete the multiPart with the reply

     connect(reply, SIGNAL(finished()),
              this, SLOT  (uploadDone()));

     connect(reply, SIGNAL(uploadProgress(qint64, qint64)),
              this, SLOT  (uploadProgress(qint64, qint64)));

}


void ApkDialog::uploadProgress(qint64 bytesSent, qint64 bytesTotal) {
    qDebug() << "---------Uploaded--------------" << bytesSent<< "of" <<bytesTotal;
}

void ApkDialog::uploadDone() {
    qDebug() << "----------Finished--------------" << reply->errorString() <<reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt();
    qDebug()<<reply;

   // reply->deleteLater();

}

2 个答案:

答案 0 :(得分:6)

我发现了错误。这是一个请求错误。 Qt文档中缺少一些东西。

这是我正在运行的代码:

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart imagePart;
    //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"version.txt\""));/* version.tkt is the name on my Disk of the file that I want to upload */ 

    QHttpPart textPart;
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\""));
    textPart.setBody("toto");/* toto is the name I give to my file in the server */

    QString apkLocation = apktextEdit->text();
    QFile *file = new QFile(apkLocation);
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

    multiPart->append(textPart);
    multiPart->append(imagePart);

    QUrl url("http://MyUrl.com");
    QNetworkRequest request(url);

    QNetworkAccessManager *networkManager= new QNetworkAccessManager;
    reply = networkManager->post(request, multiPart);
    multiPart->setParent(reply); // delete the multiPart with the reply

     connect(reply, SIGNAL(finished()),
              this, SLOT  (uploadDone()));

     connect(reply, SIGNAL(uploadProgress(qint64, qint64)),
              this, SLOT  (uploadProgress(qint64, qint64)));
}

答案 1 :(得分:0)

此处<div class="grid"> <div class="cell span-full">full</div> <div class="cell span-half">half</div> <div class="cell span-half">half</div> <div class="cell span-half">half</div> <div class="cell">single</div> <div class="cell">single</div> <div class="cell">single</div> <div class="cell">single</div> <div class="cell span-half">half</div> </div>中不需要:

filename=\"version.txt\"

您在此处设置文件名:

imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, 
                    QVariant("form-data; name=\"file\"; filename=\"version.txt\""));

它在下面的行中正常工作:

QString apkLocation = apktextEdit->text();

无论如何,非常感谢,真的很快乐!