I am trying desperately to upload a file to a server in Multipart. I have almost the same code as in the Qt docs, but the file isn't uploading to the server.
Here's what I have in my Debug:
---------Uploaded-------------- 3672 of 3672
---------Uploaded-------------- 3672 of 3672
---------Uploaded-------------- 3672 of 3672
---------Uploaded-------------- 0 of 0
----------Finished--------------
"Error transferring http://MyUrlHere.com/uploadFile - server replied: Bad Request" 400 QNetworkReplyHttpImpl(0x17589ff0)
The problem is not coming from the server because when I try uploading a file on it in multipart with a Chrome or Firefox extention it actually works!
Here is my code:
QUrl testUrl("http://MyUrlHere.com/uploadFile ");
QNetworkRequest request(testUrl);
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName("proxy");
proxy.setPort(8080);
QNetworkProxy::setApplicationProxy(proxy);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QString preview_path = "C:/Users/Desktop/image.jpg";
QHttpPart previewPathPart;
previewPathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_path\""));
previewPathPart.setBody(preview_path.toLatin1());
QString preview_name = "image.jpg";
QHttpPart previewFilePart;
previewFilePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
previewFilePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ preview_name + "\""));
QFile *file = new QFile(preview_path);
file->open(QIODevice::ReadOnly);
previewFilePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(previewPathPart);
multiPart->append(previewFilePart);
QNetworkAccessManager *networkManager= new QNetworkAccessManager;
reply = networkManager->post(request, multiPart);
multiPart->setParent(reply); // delete the multiPart with the reply
connect(reply, SIGNAL(finished()),
this, SLOT (uploadDone()));
connect(reply, SIGNAL(uploadProgress(qint64, qint64)),
this, SLOT (uploadProgress(qint64, qint64)));
}
void ApkDialog::uploadProgress(qint64 bytesSent, qint64 bytesTotal) {
qDebug() << "---------Uploaded--------------" << bytesSent<< "of" <<bytesTotal;
}
void ApkDialog::uploadDone() {
qDebug() << "----------Finished--------------" << reply->errorString() <<reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug()<<reply;
// reply->deleteLater();
}