如何为我正在使用QNetworkAccessManager上传的图像设置QNetworkRequest :: ContentTypeHeader和QNetworkRequest :: ContentLengthHeader?

时间:2013-08-04 17:41:24

标签: qt post qnetworkaccessmanager

我正在尝试使用Qt的QNetworkAccessManager类使用POST方法将图像上传到apache服务器。我没有得到的是,如何为图像设置合适的QNetworkRequest :: ContentTypeHeader和QNetworkRequest :: ContentLengthHeader?如果ContentTypeHeader是“multipart / form-data”,那么边界应该设置为什么?

示例代码:

    data = new QFile("/home/darshan/aindra/1.png", this);
    if (data->open(QIODevice::ReadOnly))
    {
        manager = new QNetworkAccessManager();
        req.setUrl(QUrl(upload_url));
        //space for req.setHeader() - contenttypeheader
        //space for req.setHeader() - contentlengthheader
        //reply = manager->post(req, QByteArray);
        connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(requestFinished(QNetworkReply*)));
        connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(uploadProgress(qint64, qint64)));
    }
    else
    {
        qDebug() << "Could not open file to FTP";
    }

1 个答案:

答案 0 :(得分:2)

自Qt 4.8起,您可以使用QHttpMultipart上传QNetworkAccessManager的文件。

http://doc.qt.io/qt-4.8/qhttpmultipart.html