Results 1 to 4 of 4

Thread: QNetworkRequest file upload -- please help

  1. #1
    Join Date
    Jul 2009
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default QNetworkRequest file upload -- please help

    Hey folks - thank you so much for your help. Most of this is pretty new to me, and I have been able to only get so far. Could someone familiar with QNetworkRequest and php please help me see where I am going astray?

    Patching together several other attempts by various people I came up with this code to upload a file via POST to a php upload script. My code:

    ... from on_button_clicked() ...

    Qt Code:
    1. QString bound,data,crlf;
    2. QByteArray dataToSend;
    3.  
    4. bound="---------------------------7d935033608e2";
    5. crlf=0x0d;
    6. crlf+=0x0a;
    7. data="--"+bound+crlf+"Content-Disposition: form-data; name=\"uploadedFile\";";
    8. data+="filename=\"Contact List.csv\"";
    9. data+=crlf+"Content-Type: text/plain"+crlf+crlf;
    10. data+=inputFile.readAll(); // insert content of text file
    11. data+=crlf+"--"+bound+"--"+crlf;
    12. dataToSend.insert(0,data); // correct/needed?
    13.  
    14. // request init
    15. QNetworkRequest request(QUrl("http://edm.localhost/uploader.php"));
    16. // need to set headers here?
    17. reply=manager.post(request,dataToSend); // perform post request
    18.  
    19. // connections
    20. connect(reply,SIGNAL(uploadProgress(qint64,qint64)),
    21. SLOT(mySetValue(qint64,qint64)));
    22. connect(reply,SIGNAL(finished()),SLOT(replyFinished())); // reply finished - close file
    To copy to clipboard, switch view to plain text mode 



    and my php script is:
    html Code:
    1. <?php
    2. $target_path = "";
    3.  
    4. if($_FILES['uploadedFile']['name'])
    5. $target_path= $target_path.basename($_FILES['uploadedFile']['name']);
    6. else
    7. echo "no file name";
    8. echo "<br>";
    9.  
    10. if(move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $target_path))
    11. echo "The file ".basename( $_FILES['uploadedFile']['name'])." has been uploaded";
    12. else
    13. echo "There was an error uploading the file, please try again!";
    14. ?>
    To copy to clipboard, switch view to plain text mode 

    The program seems to function, i.e. no errors or unexpected quitting and the progress bar updates through 100%, but I never receive the uploaded file in my upload directory (or elsewhere) as I do when I access this script through the following HTML form.

    html Code:
    1. <form enctype="multipart/form-data" action="uploader.php" method="POST">
    2. <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
    3. Choose a file to upload: <input name="uploadedFile" type="file" /><br />
    4. <input type="submit" value="Upload File" />
    5. </form>
    To copy to clipboard, switch view to plain text mode 

    I'm running a QT4.5 (coding in QTCreator 4.5.2) on an UBUNTU 8.10 LAMP server. Thank you so much for any help!
    Last edited by wysota; 13th July 2009 at 09:20. Reason: missing [code] tags

  2. #2
    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: QNetworkRequest file upload -- please help

    Compare the contents of the request that goes out in both cases using a network sniffer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Runtime Technologies (13th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: QNetworkRequest file upload -- please help

    UPDATE:

    Using WireShark I have been able to decipher that the $_FILES variable is just not being populated by the functioning of my QT application. I have read that this could potentially derive from an insufficiently high max_upload_size in php.ini, but with even the smallest file I see the same response.

    Using WireShark I also "cleaned up" the data sent to the php script. I'm still confused, though, about the necessity of certain headers. The arrangement I have now appears to work from within my program, but WireShark responds with my coded php response to !$_FILES (echo "no file" and a further "Bad Request ... Your browser sent a request that this server could not understand." in a continuation.

    Qt Code:
    1. // file init
    2. //inputFile.setFileName("test.csv");
    3. inputFile.setFileName("main.cpp");
    4. if(!inputFile.open(QFile::ReadOnly|QIODevice::Text)) {
    5. ui->textEdit->append("File failed to open - contact system administrator.");
    6.  
    7. } else {
    8. QString boundary,data,crlf,fileByteSize;
    9. QByteArray dataToSend;
    10.  
    11. boundary="-----------------------------7d935033608e2";
    12. crlf=0x0d;
    13. crlf+=0x0a;
    14.  
    15. data=crlf+"--"+boundary+crlf; // start delimiter
    16. data+="Content-Disposition: form-data; name=\"MAX_FILE_SIZE\";"+crlf+crlf;
    17. data+=fileByteSize.setNum(inputFile.size()+1);
    18. data+=crlf+"--"+boundary+crlf;
    19. data+="Content-Disposition: form-data; name=\"username\";"+crlf+crlf+"username"+crlf; // !! effect username !!
    20. data+=crlf+"--"+boundary+crlf;
    21. data+="Content-Disposition: form-data; name=\"password\";"+crlf+crlf+"password"+crlf; // !! effect password !!
    22. data+=crlf+"--"+boundary+crlf;
    23. data+="Content-Disposition: form-data; name=\"uploadedFile\"; filename=\"ContactList.csv\";"+crlf;
    24. data+="Content-Type: text/plain"+crlf+crlf+inputFile.readAll()+crlf;
    25. data+="--"+boundary+"--"+crlf; // stop delimiter
    26. dataToSend.insert(0,data); // convert to byte array for request
    27.  
    28. ui->textEdit->insertPlainText(dataToSend.data()); // update ui for my own sake
    29.  
    30. // request init
    31. QNetworkRequest request(QUrl("http://edm.localhost/uploader.php"));
    32. //request.setRawHeader("Content Type","text/plain");
    33.  
    34. //QString contentType="multipart/form-data; boundary=\""+boundary+"\"";
    35. //QString host="http://edm.localhost/";
    36.  
    37. //request.setRawHeader("Host",host.toAscii());
    38. // if (userAgentS!="") request.setRawHeader("User-Agent", userAgentS.toAscii());
    39. // if (refererS!="") request.setRawHeader("Referer", refererS.toAscii());
    40. //request.setHeader(QNetworkRequest::ContentTypeHeader,contentType.toAscii());
    41. //request.setHeader(QNetworkRequest::ContentLengthHeader,QVariant(inputFile.size()).toString());
    42. //request.setUrl(QUrl("http://edm.localhost"));
    43.  
    44. //request.setRawHeader("Content Disposition","form-data; name=\"uploadedFile\"; filename=\"DownloadedContactList.csv\"");
    45. //request.setRawHeader("Content Length",dataToSend);//fileByteSize.setNum(inputFile.size()));
    46. //request.setHeader(QNetworkRequest::ContentTypeHeader,"multipart/form-data; boundary: -----------------------------7d935033608e2\n");
    47. request.setHeader(QNetworkRequest::ContentLengthHeader,inputFile.size());
    48.  
    49. reply=manager.post(request,dataToSend); // perform post request
    50.  
    51. // QByteArray data = reply->readAll();
    52. // QTextStream out(&data);
    53. // QString file = out.readAll();
    54.  
    55. ui->textEdit->append(reply->readAll());
    56.  
    57. // connections
    58. connect(reply,SIGNAL(uploadProgress(qint64,qint64)),SLOT(mySetValue(qint64,qint64)));
    59. connect(reply,SIGNAL(finished()),SLOT(replyFinished())); // reply finished - close file
    To copy to clipboard, switch view to plain text mode 


    Any insight would be greatly appreciated.
    Last edited by wysota; 13th July 2009 at 22:47. Reason: missing [code] tags

  5. #4
    Join Date
    Jul 2009
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 1 Post

    Default Re: QNetworkRequest file upload -- please help

    Woohoo! Got it .... for posterity sake and for those with similar difficulty, the finished code is:

    Qt Code:
    1. QString boundary,data,crlf,fileByteSize;
    2. QByteArray dataToSend; // byte array to be sent in POST
    3.  
    4. // data boundary declerations
    5. crlf=0x0d;
    6. crlf+=0x0a;
    7. boundary="-----------------------------7d935033608e2";
    8.  
    9. data=boundary;
    10. data+="Content-Disposition: form-data; name=\"uploadedFile\"; filename=\"ContactList.csv\";"+crlf;
    11. data+="Content-Type: text/csv"+crlf+crlf+inputFile.readAll();
    12. data+=boundary;
    13. dataToSend=data.toAscii(); // convert data string to byte array for request
    14.  
    15. // request init
    16. QNetworkRequest request(QUrl("http://edm.localhost/uploader.php"));
    17. request.setRawHeader("Content-Type","multipart/form-data; boundary=\"-----------------------------7d935033608e2\"");
    18. request.setHeader(QNetworkRequest::ContentLengthHeader,dataToSend.size());
    19. reply=manager.post(request,dataToSend); // perform POST request
    20.  
    21. // connections
    22. connect(reply,SIGNAL(uploadProgress(qint64,qint64)),SLOT(mySetValue(qint64,qint64)));
    23. connect(reply,SIGNAL(finished()),SLOT(replyFinished())); // reply finished - close file
    To copy to clipboard, switch view to plain text mode 

    and for those who don't see the immediate differance, as I didn't within the request header, 'Content Type: ..." must actually be written as 'Content-Type: ...".

    I spent 20 hours on a hyphen! If at first you don't succeed, try and try again, eh.

    Thanks wysota ... the network sniffer was essential. Thanks QT Centre Forum.

  6. The following 2 users say thank you to Runtime Technologies for this useful post:

    melon (29th July 2010), pedromorgan (17th September 2009)

Similar Threads

  1. How to upload file to HTTP server (POST Method)
    By Alex Snet in forum Qt Programming
    Replies: 8
    Last Post: 24th January 2011, 22:49
  2. Replies: 5
    Last Post: 20th January 2009, 14:11
  3. File Binary Upload QHttp find the bug/s
    By patrik08 in forum Qt Programming
    Replies: 13
    Last Post: 10th June 2008, 07:51
  4. QHttp::post() - cannot upload file
    By arunredi in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2008, 12:13
  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

Tags for this Thread

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.