6

我正在使用 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") . "\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'];

我的进度槽显示了大致正确的上传字节数。

但输出显示:

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

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

4

1 回答 1

9

解决了!

感谢这个问题的提示 - 我在 QHttpPart 的内容配置中添加了“文件名”,它现在按预期上传。我认为文件名是可选的,但在这种情况下似乎有效。希望这对其他人有帮助!

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 + "\""));
于 2013-06-24T11:31:40.243 回答