我正在尝试使用QNetworkAccessManager上传文件,但我总是得到一个错误(错误传输url -服务器回答:坏请求)。下面是我的代码
QString name = "Simple.txt";
QString type = "text/plain; charset=utf-8";
QString uploadUrl = "myuploadUrl";
// setup the multipart request
QString bound="---------------------------723690991551375881941828858";
QByteArray data(QString("--"+bound+"rn").toLatin1());
// write the file using standard method for multipart file upload
data += "Content-Disposition: form-data; name="file"; filename=""+name.toLatin1()+""rn";
data += "Content-Type: "+type.toLatin1()+"rnrn";
data += "Hello, I am simple file";
data += "rn";
data += "--" + bound;
qDebug() << data;
// make the request with appropriate headers
QNetworkRequest request(QUrl(uploadUrl));
request.setRawHeader(QByteArray("Content-Type"),QString("multipart/form-data; boundary=" + bound).toLatin1());
request.setRawHeader(QByteArray("Content-Length"), QString::number(data.length()).toLatin1());
QNetworkReply *reply = networkManager->post(request,data);
QObject::connect(reply, &QNetworkReply::finished, this, FileUploader::requestFinished);
然而,运行这个python脚本,它应该做同样的事情,工作。
import requests
import json
file_name = "Simple.txt"
uploadUrl = "myUploadUrl";
resp = requests.post(uploadUrl, data=r["data"], files={"file": open(file_name, "rb")})
print (resp);
通过在数据末尾添加data += "rn"
来修复此问题。
使用QT内部构件代替手动创建请求。你可以试试这段代码,它正在为我工作。
void OfflineLogHandler::uploadOfflineLogFile(QString filePath){
QUrl targateUlr(APIUrls::getInstance()->getFlightLogUploadURL()); //pass URL here
APIUrls *apiUrls = APIUrls::getInstance();
qDebug()<<"Target upload offline log url:"<<targateUlr;
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart formHeader;
formHeader.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=""; filename=""+filePath.section("/",-1)+"""));
if(filePath.contains(".json"))
formHeader.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
else if(filePath.contains(".csv"))
formHeader.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/csv"));
QFile *file = new QFile(filePath);
file->open(QIODevice::ReadOnly);
formHeader.setBodyDevice(file);
multiPart->append(formHeader);
file->setParent(multiPart);
QNetworkRequest request;
request.setUrl(targateUlr);
QString CredData = apiUrls->getCredentials();
QString headerData = apiUrls->getApiAuthorizationType() + CredData;
qDebug()<<"access:"<<headerData.toLocal8Bit();
request.setRawHeader( "Authorization", headerData.toLocal8Bit() );
request.setRawHeader("CustomeData1","Data1");
request.setRawHeader("CustomeData2","Data2");
request.setHeader(QNetworkRequest::ContentTypeHeader,QString("multipart/form-data; boundary="+multiPart->boundary()).toLatin1());
QNetworkAccessManager *restclient; //in class
restclient = new QNetworkAccessManager(); //constructor
disconnect(restclient,SIGNAL(finished(QNetworkReply*)), this, nullptr);
connect(restclient, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleFileUploadResponse(QNetworkReply*)));
networkReply = restclient->post(request,multiPart);
connect(networkReply,SIGNAL(uploadProgress(qint64, qint64)),this,SLOT(fileUprogress(qint64, qint64)));
qDebug()<<"net:"<<networkReply;
}