Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading a file in Multipart form data in Qt5

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();

}

1 Answers

I found the error. It was a request error. there is a little thing missing in Qt docs.

Here is my code that is working :

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart imagePart;
    //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"version.txt\""));/* version.tkt is the name on my Disk of the file that I want to upload */ 

    QHttpPart textPart;
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\""));
    textPart.setBody("toto");/* toto is the name I give to my file in the server */

    QString apkLocation = apktextEdit->text();
    QFile *file = new QFile(apkLocation);
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

    multiPart->append(textPart);
    multiPart->append(imagePart);

    QUrl url("http://MyUrl.com");
    QNetworkRequest request(url);

    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)));
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!