使用Qt分段上传时出错

Error during multipart upload using Qt

我正在尝试使用 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+"\r\n").toLatin1());

// write the file using standard method for multipart file upload
data += "Content-Disposition: form-data; name=\"file\"; filename=\""+name.toLatin1()+"\"\r\n";
data += "Content-Type: "+type.toLatin1()+"\r\n\r\n";

data += "Hello, I am simple file";
data += "\r\n";
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 += "\r\n" 解决。

使用 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;
}