QHttpMultiPart not working
Unsolved
General and Desktop
-
I am trying to upload file (to Facebook) using QHttpMultiPart. Below is my code. This always gives me the following error.
"{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324,"fbtrace_id":"Fsi2f\/H6ggp"}}"
However if I write the data manually without using QHttpMultiPart, the file is uploaded successfully (second snippet code)
QFile file(m_fileName); QFileInfo fileInfo(m_fileName); // to get info on the file bool open = file.open(QIODevice::ReadOnly); if(open){ // start the upload QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart privacyPart; privacyPart.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain; charset=UTF-8"); privacyPart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"privacy\""); privacyPart.setBody("{\"value\":\"FRIENDS_OF_FRIENDS\"}"); QHttpPart filePart; filePart.setHeader(QNetworkRequest::ContentTypeHeader, "image/png"); filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"source\"; filename = \"" + fileInfo.fileName() + "\""); filePart.setRawHeader("Content-Transfer-Encoding", "binary"); filePart.setBody(file.readAll()); multiPart->append(privacyPart); multiPart->append(filePart); QNetworkRequest request(url); request.setRawHeader("Authorization", "OAuth " + accessToken.toUtf8()); QNetworkReply* reply = m_networkAccessManager->post(request, multiPart); multiPart->setParent(reply); // delete multipart with reply // connect signals connect(reply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64))); connect(reply, SIGNAL(finished()), this, SLOT(replyFinished())); } else{ qDebug() << "Unable to open file" << fileName(); qDebug() << file.errorString(); }
Second snippet (writing data manually)
// start the upload QString boundary("----eidevelop1010101010"); QString contentType("multipart/form-data; boundary="+boundary); QByteArray data; data += QString("--"+boundary + "\r\n").toUtf8(); data += QString("Content-Disposition: form-data; name=\"source\";filename="+fileInfo.fileName()+"\r\n").toUtf8(); data += QString("Content-Type: image/jpeg\r\n\r\n").toUtf8(); data += file.readAll(); data += "\r\n"; data += QString("--" + boundary + "\r\n").toUtf8(); data += QString("--" + boundary + "\r\n").toUtf8(); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(contentType)); request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(QString::number(data.size()))); QNetworkReply* reply = m_networkAccessManager->post(request, data); // connect signals connect(reply, SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64))); connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));