gpt4 book ai didi

qt - 在 Qt5 的多部分表单数据中上传文件

转载 作者:行者123 更新时间:2023-12-04 20:02:54 26 4
gpt4 key购买 nike

我正在拼命尝试将文件上传到 Multipart 中的服务器。我的代码几乎与 Qt 文档中的代码相同,但文件未上传到服务器。

这是我的调试中的内容:

---------Uploaded-------------- 3672 of 3672

---------Uploaded-------------- 3672 of 3672

---------Uploaded-------------- 3672 of 3672

---------Uploaded-------------- 0 of 0

----------Finished--------------

"Error transferring http://MyUrlHere.com/uploadFile - server replied: Bad Request" 400 QNetworkReplyHttpImpl(0x17589ff0)

问题不是来自服务器,因为当我尝试使用 Chrome 或 Firefox 扩展在服务器上上传一个文件时,它确实有效!

这是我的代码:

     QUrl testUrl("http://MyUrlHere.com/uploadFile ");
QNetworkRequest request(testUrl);
QNetworkProxy proxy;

proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName("proxy");
proxy.setPort(8080);
QNetworkProxy::setApplicationProxy(proxy);

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

QString preview_path = "C:/Users/Desktop/image.jpg";
QHttpPart previewPathPart;
previewPathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_path\""));
previewPathPart.setBody(preview_path.toLatin1());

QString preview_name = "image.jpg";

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

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

connect(reply, SIGNAL(finished()),
this, SLOT (uploadDone()));

connect(reply, SIGNAL(uploadProgress(qint64, qint64)),
this, SLOT (uploadProgress(qint64, qint64)));

}


void ApkDialog::uploadProgress(qint64 bytesSent, qint64 bytesTotal) {
qDebug() << "---------Uploaded--------------" << bytesSent<< "of" <<bytesTotal;
}

void ApkDialog::uploadDone() {
qDebug() << "----------Finished--------------" << reply->errorString() <<reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug()<<reply;

// reply->deleteLater();

}

最佳答案

我发现了错误。这是一个请求错误。 Qt 文档中缺少一些东西。

这是我的有效代码:

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
//imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"version.txt\""));/* version.tkt is the name on my Disk of the file that I want to upload */

QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\""));
textPart.setBody("toto");/* toto is the name I give to my file in the server */

QString apkLocation = apktextEdit->text();
QFile *file = new QFile(apkLocation);
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

multiPart->append(textPart);
multiPart->append(imagePart);

QUrl url("http://MyUrl.com");
QNetworkRequest request(url);

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

connect(reply, SIGNAL(finished()),
this, SLOT (uploadDone()));

connect(reply, SIGNAL(uploadProgress(qint64, qint64)),
this, SLOT (uploadProgress(qint64, qint64)));
}

关于qt - 在 Qt5 的多部分表单数据中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38179706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com