QHttpMultiPart: post文件到PHP脚本。

时间:2022-09-15 16:34:39

I am working in Qt 5 and struggling with a multipart upload. My script is as close to the docs as possible:

我在Qt 5工作,并在与多部分上传斗争。我的脚本尽可能接近文档:

 QUrl testUrl("http://localhost/upload/test.php");
 QNetworkRequest request(testUrl);


 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());

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

 if (!file->exists()) {
     emit error(tr("Upload Error. File does not exist: ") + preview_path);
     return;
 }
 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);

 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)));

then my uploadReply() slot just prints the reply:

然后我的uploadReply()槽只打印回复:

QString data = (QString) reply->readAll();
qDebug() << data;

I have made the upload script is as simple as possible and running in XAMPP with Apache/2.2.21 and PHP 5.3.8. My upload_max_filesize and post_max_size are 1000M.

我已经让上传脚本尽可能简单,在XAMPP中运行Apache/2.2.21和PHP 5.3.8。我的upload_max_filesize和post_max_size是1000M。

echo "preview_path: " . (isset($_POST['preview_path']) ? $_POST['preview_path'] : "not set") . "\r\n";
echo "preview_file exists: " . (isset($_POST['preview_file']) ?  "true" : "false" ). "\r\n";
echo '$_FILES: ';
print_r($_FILES);
echo "preview_file content: " . $_POST['preview_file'];

my progress slot shows the roughly the correct number of bytes being uploaded.

我的进程插槽显示了被上传的字节的正确数量。

But the output shows:

但输出显示:

 preview_path: C:/preview.jpg
 preview_file exists: true
 $_FILES: Array
 (
 )
 preview_file content: ????

It seems like the bytes are being submitted as $_POST variable rather than a $_FILE as they should be? There is no error in the apache log. How can I debug this?

看起来字节被提交为$_POST变量而不是$_FILE,因为它们应该是这样的。apache日志中没有错误。我如何调试这个?

1 个解决方案

#1


8  

Solved!

解决了!

Thanks to this question for the hint - I added 'filename' to the content disposition for the QHttpPart and it now uploads as expected. I thought filename was optional but seems to work in this case. Hope this helps someone else!

由于这个提示问题,我在QHttpPart的内容配置中添加了“filename”,它现在像预期的那样上传了。我认为filename是可选的,但在这种情况下似乎行得通。希望这能帮助别人!

QString preview_name = "preview.jpg";

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

#1


8  

Solved!

解决了!

Thanks to this question for the hint - I added 'filename' to the content disposition for the QHttpPart and it now uploads as expected. I thought filename was optional but seems to work in this case. Hope this helps someone else!

由于这个提示问题,我在QHttpPart的内容配置中添加了“filename”,它现在像预期的那样上传了。我认为filename是可选的,但在这种情况下似乎行得通。希望这能帮助别人!

QString preview_name = "preview.jpg";

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