QHttpMultiPart上传文件边界问题

QHttpMultiPart upload file boundary issue

我正在尝试使用 QHttpMultiPart 将一些文件上传到服务器。下面的版本文件只是一个例子。由于某些原因,Qt 会自动在文件中添加边界。但是,我真正上传的是.tar.gz个文件,如果加上这样的边界,它们就会被认为是损坏的。

    int main(int argc, char *argv[])
    {

        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);

        QNetworkAccessManager *netManager = new QNetworkAccessManager();
        HTTPFirmwareUpgrader upgrader(netManager,"http://mytest.com");
        upgrader.upgradeCIU();

        return app.exec();
    }

    void HTTPFirmwareUpgrader::upgradeCIU()
    {
        QString filename = "version";
        QString filePath = QString("C:/Users/User/Desktop/HTTP/%1").arg(filename);
        qDebug() << uploadFirmware(filename, filePath);
    }
    
    bool HTTPFirmwareUpgrader::uploadFirmware(const QString &filename, const QString &filePath)
    {
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
        QHttpPart myPart;
        QFile *file = new QFile(filePath);
        if(!file->exists())
            qWarning() << "File DOES NOT exists";
    
        file->open(QIODevice::ReadOnly);
        myPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data"));
        myPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"version\"; filename=\"version\""));
        myPart.setBodyDevice(file);
        file->setParent(multiPart);
    
        multiPart->append(ciu);
    
        QByteArray resp = this->post(createUploadRequest(QString("filename=%1").arg(filename)),
                                     multiPart, file);
        qDebug() << "Upload Filrmware " << resp;
    
        return resp != "";
    }

    QByteArray HTTPFirmwareUpgrader::post(QUrl url, QHttpMultiPart *multiPart, QFile *file)
    {
        QNetworkRequest request;
        QEventLoop loop;
        request.setUrl(url);

        request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data"));
        QNetworkReply *reply = m_manager->post(request, multiPart);
        connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec();
        multiPart->setParent(reply);
        reply->deleteLater();

        return reply->readAll();
}
 

这是原始“版本”文件: enter image description here

这是传输的“版本”文件: enter image description here

如果我不为请求设置header,传输文件的大小将为0。

有人知道我该怎么做吗?

问题解决了!!! 原来我上传文件的方式不对。我需要使用的是 QIODevice 而不是 QMultipart。