QHttpMultiPart 上传多个文件

Posted

技术标签:

【中文标题】QHttpMultiPart 上传多个文件【英文标题】:QHttpMultiPart upload multiple files 【发布时间】:2014-04-28 14:55:59 【问题描述】:

我正在尝试通过我的应用程序将多个文件上传 (POST) 到网络服务器。 php端工作,因为我使用cURL上传,但我想通过Qt做同样的事情。

我看过这个example,但我不确定如何将其转换为上传多个文件。

QUrl url(connServer + "/api.php");
QNetworkRequest request(url);

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

QString preview_path  = "C:/preview.jpg";
QHttpPart previewPathPart;
previewPathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_path\""));
previewPathPart.setBody(preview_path.toLatin1()); /* <-------------- I'm stumped here */
multiPart->append(previewPathPart);

for (int i=0;i<list.size();i++) 
    qDebug(list[i].toStdString().c_str());
    continue;

    QString filename = list[i];

    QHttpPart previewFilePart;
    previewFilePart.setHeader(QNetworkRequest::ContentTypeHeader,    QVariant("image/jpeg"));
    previewFilePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ filename + "\""));
    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(previewFilePart);


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

connect(reply, SIGNAL(finished()), this, SLOT  (uploadReply()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT  (uploadError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT  (uploadProgress(qint64, qint64)));

QHttpMultiPart 的哪些部分应该设置一次,每个文件的哪些部分需要设置多次?

【问题讨论】:

【参考方案1】:

上面的代码很接近,但我只需要弄清楚哪些代码指定了一次性变量,哪些代码指定了文件。此代码可用于上传多个文件。但是......它一次只能上传20个文件。不确定这是否是 Qt 或其他东西的限制。我的等效 Perl/cURL 程序可以毫无问题地将数百个文件上传到同一个 PHP 页面。

QUrl url(connServer + "/api.php");
QNetworkRequest request(url);

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

QHttpPart loginPart;
/* username */
loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"u\""));
loginPart.setBody(connUsername.toLatin1());
multiPart->append(loginPart);
/* password */
loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"p\""));
loginPart.setBody(connPassword.toLatin1());
multiPart->append(loginPart);
/* action */
loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"action\""));
loginPart.setBody("UploadDICOM");
multiPart->append(loginPart);

/* loop through the list of files */
ui->progUpload->setRange(0,100);
for (int i=0;i<list.size();i++) 
    qDebug("UploadFileList [%d] [%s]", i, list[i].toStdString().c_str());
    QFile *file = new QFile(list[i]);
    QHttpPart filePart;
    /* important that the files[] variable have the brackets, for PHP to interpret correctly */
    filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"files[]\"; filename=\""+ file->fileName() + "\""));

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


QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
QNetworkReply* reply = networkManager->post(request, multiPart);
multiPart->setParent(reply); // delete the multiPart with the reply
connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT  (uploadError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT  (uploadProgress(qint64, qint64)));

【讨论】:

以上是关于QHttpMultiPart 上传多个文件的主要内容,如果未能解决你的问题,请参考以下文章

QT QHttpMultiPart上传图片

QHttpMultiPart:将文件发布到 PHP 脚本

输入文件中的多个文件上传问题

使用 NetworkManager 上传文件不起作用

org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to org.springframwork.web.mult..

为什么上传到谷歌硬盘时没有设置文件名?