How to set QNetworkRequest::ContentTypeHeader and QNetworkRequest::ContentLengthHeader for an image I'm uploading using QNetworkAccessManager?

StackOverflow https://stackoverflow.com/questions/18045713

  •  23-06-2022
  •  | 
  •  

Frage

I'm trying to upload an image to an apache server using Qt's QNetworkAccessManager class using the POST method. What I don't get is, how to set appropriate QNetworkRequest::ContentTypeHeader and QNetworkRequest::ContentLengthHeader for an image? If the ContentTypeHeader is "multipart/form-data", what should the boundary be set as?

Sample Code:

    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";
    }
War es hilfreich?

Lösung

Since Qt 4.8, you can use QHttpMultipart to upload files with QNetworkAccessManager.

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top