QHttpMultiPart:将文件发布到PHP脚本 [英] QHttpMultiPart: post files to PHP script

查看:147
本文介绍了QHttpMultiPart:将文件发布到PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Qt 5中工作,并且需要分段上传.我的脚本尽可能靠近 docs :

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

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

然后我的uploadReply()插槽仅打印答复:

then my uploadReply() slot just prints the reply:

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

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

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.

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.

但是输出显示:

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

似乎字节是作为$ _POST变量而不是应有的$ _FILE提交的? apache日志中没有错误.我该如何调试?

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?

推荐答案

已解决!

感谢此问题作为提示-我在"QHttpPart的内容处置,现在可以按预期上传.我以为文件名是可选的,但在这种情况下似乎可以工作.希望这对其他人有帮助!

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!

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 + "\""));

这篇关于QHttpMultiPart:将文件发布到PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆