QHttpMultiPart: post文件到PHP脚本



我正在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)));

那么我的uploadReply()槽只打印回复:

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

我已经使上传脚本尽可能简单,并在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") . "rn";
echo "preview_file exists: " . (isset($_POST['preview_file']) ?  "true" : "false" ). "rn";
echo '$_FILES: ';
print_r($_FILES);
echo "preview_file content: " . $_POST['preview_file'];

my progress slot显示上传的字节数大致正确。

但是输出显示:

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

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

解决!

感谢这个问题的提示-我为QHttpPart的内容配置添加了'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 + """));

相关内容

  • 没有找到相关文章

最新更新