Results 1 to 6 of 6

Thread: file upload using QNetworkRequest & QNetworkAccessManager

  1. #1
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default file upload using QNetworkRequest & QNetworkAccessManager

    Hi,

    Well, does someone has an example of file upload with
    QNetworkRequest and QNetworkAccessManager.

    Qt is really laking documentation on that...


    Best regards,
    Oscar

  2. #2
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: file upload using QNetworkRequest & QNetworkAccessManager

    Here is one of the tries I did for the upload...
    It doesn't work with the Qt code.
    However, with a html forms, the PHP script get the uploaded file correctly...

    Qt Code:
    1. QNetworkAccessManager *manager;
    2. manager = new QNetworkAccessManager();
    3. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinishedSlot(QNetworkReply*)));
    4. QNetworkRequest req;
    5. req.setUrl(QUrl("http://mywebsite.com/upload.php"));
    6. req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data, Boundary: -----------------1111553253537452673632\n");
    7. req.setHeader(QNetworkRequest::ContentLengthHeader, 4);
    8. manager->post(req, "-----------------1111553253537452673632\nabcd");
    To copy to clipboard, switch view to plain text mode 

    Best regards,
    Oscar

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: file upload using QNetworkRequest & QNetworkAccessManager

    I think at least content-length is wrong. It should contain all you send as the contents of POST. Currently the server probably only interprets "----" as these are the four first bytes of the content.

  4. #4
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: file upload using QNetworkRequest & QNetworkAccessManager

    Thank you wysota for your help,

    I tried a length of 43 characters (39 characters of header + 4 characters of datas), but It doesn't work better...

    Qt Code:
    1. QNetworkRequest req;
    2. req.setUrl(QUrl("https://mywebsite.com/upload.php"));
    3. req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=-----------------1111553253537452673632"); //39
    4. req.setHeader(QNetworkRequest::ContentLengthHeader, 43);
    5. manager->post(req, "-----------------1111553253537452673632abcd");
    To copy to clipboard, switch view to plain text mode 

    He is the remote PHP script:

    Qt Code:
    1. <?
    2. $fhLog = fopen("log.txt",'a');
    3. foreach ($_FILES as $myfile) {
    4. fwrite($fhLog, $myfile['tmp_name']);
    5. }
    6. fclose($fhLog);
    7. ?>
    To copy to clipboard, switch view to plain text mode 

    As I said, when using a basic upload form, the PHP script is getting correctly the uploaded file, but with QT, the PHP script is called well, I can send HTTP headers fields but I don't find a way to upload files...

    Sending just POST fields with QT to a PHP script also works (I then get the fields in the PHP using $_POST['myField]). The only problem is with uploaded files... Grrrr...

    I should do a wireshark capture to see the QT request content but It's not really easy...

    There must be some examples on the web, but I can't find anyone...

    I hope someone can give me some more help.

    Thanks.
    Oscar
    Last edited by oscar; 14th December 2008 at 09:11.

  5. #5
    Join Date
    Nov 2007
    Posts
    53
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: file upload using QNetworkRequest & QNetworkAccessManager

    HTTP upload works like a charm !

    Here is a quick example with the upload of a JPEG image.

    It matches such an HTML form

    Qt Code:
    1. <form method="post" enctype="multipart/form-data" action="upload.php">
    2. <p>
    3. <input type="file" name="fichier" size="30">
    4. <input type="submit" name="upload" value="Uploader">
    5. </p>
    6. </form>
    To copy to clipboard, switch view to plain text mode 

    And here is the Qt code :

    Qt Code:
    1. QHttp *http = new QHttp(this);
    2. QString boundary = "---------------------------193971182219750";
    3.  
    4. QByteArray datas(QString("--" + boundary + "\r\n").toAscii());
    5. datas += "Content-Disposition: form-data; name=\"fichier\"; filename=\"DSCF1055.jpg\"\r\n";
    6. datas += "Content-Type: image/jpeg\r\n\r\n";
    7.  
    8. QFile file("C:\\DSCF1055.jpg");
    9. if (!file.open(QIODevice::ReadOnly))
    10. return;
    11.  
    12. datas += file.readAll();
    13. datas += "\r\n";
    14. datas += QString("--" + boundary + "\r\n").toAscii();
    15. datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
    16. datas += "Uploader\r\n";
    17. datas += QString("--" + boundary + "--\r\n").toAscii();
    18.  
    19. QHttpRequestHeader header("POST", "/upload.php");
    20. header.setValue("Host", "www.myhost.com");
    21. header.setValue("Content-Type", "multipart/form-data; boundary=" + boundary);
    22. header.setValue("Content-Length", QString::number(datas.length()));
    23.  
    24. http->setHost("www.myhost.com");
    25. http->request(header, datas);
    To copy to clipboard, switch view to plain text mode 

    If you want extra parameters, the best thing is to use a sniffer as WireShark to see how it is embedded at the end of the datas (where we have the value "Uploader" of the submit button there).

    But you can see how Content-Length is computed and that the boundary is always prefixed in datas part with "--"

  6. #6

    Default Re: file upload using QNetworkRequest & QNetworkAccessManager

    You can find one solution for poting to web forms (with file uploads) here: http://www.tuckdesign.com/sources/Qt

Similar Threads

  1. File Binary Upload QHttp find the bug/s
    By patrik08 in forum Qt Programming
    Replies: 13
    Last Post: 10th June 2008, 07:51
  2. QHttp::post() - cannot upload file
    By arunredi in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2008, 12:13
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. how to use qftp to upload file in just one procedure?
    By cxl2253 in forum Qt Programming
    Replies: 4
    Last Post: 23rd April 2007, 09:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.