Results 1 to 13 of 13

Thread: Upload request

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Upload request

    If you want examples, you can use something like Etherpeek with a web page which allows you to upload data from your pc (eg. imageshack).

    However, this will not tell you what YOUR server supports.

    You can also use it once you have written your code to ensure it is sending the correct data. Its much better than trying to code "blind".

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Upload request

    http://www.w3.org/TR/html401/interact/forms.html

    In particular, the section on "Form content types". For a large file (not text or more than a few tens of bytes) you typically want a Content-Type of "multipart/form-data" rather than "application/x-www-form-urlencoded" in your headers. Your request payload then needs to be encoded suitably.

  3. #3
    Join Date
    Mar 2011
    Location
    New Delhi, India
    Posts
    31
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60
    Thanks
    6

    Default Re: Upload request

    Thanks all. I did 2 mistakes one was not encoding the data to be sent and other was (silly) by not specifying the correct URL of the page. The code that works for me is below:


    Qt Code:
    1. uploadmanager::uploadmanager()
    2. {
    3. connect(&manager, SIGNAL(finished(QNetworkReply*)),
    4. SLOT(uploadFinished(QNetworkReply*)));
    5. }
    6.  
    7.  
    8. void uploadmanager::doUpload()
    9. {
    10. QString message_d = QString("Unable to open file");
    11. args<<"E:\\SMS\\spam_freq.csv";
    12.  
    13. foreach (QString arg, args) {
    14.  
    15. QFile file(arg);
    16.  
    17. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    18. {
    19. QMessageBox::information(this, "Error", message_d);
    20. return;
    21. }
    22.  
    23. file.open(QIODevice::ReadOnly | QIODevice::Text);
    24. QByteArray data(file.readAll());
    25. QByteArray sdata;
    26. QUrl params;
    27. params.addQueryItem("data", data); //I wasn't encoding the data, one error was here
    28. sdata.append(params.toString());
    29.  
    30.  
    31. file.close();
    32.  
    33.  
    34.  
    35. QNetworkRequest request;
    36.  
    37. request.setUrl(QUrl("http://myserv.com/smsspam/smsspam.php")); // i committed a silly blunder here by not including th php page in URL ( which i came to know by checking the reply content)
    38.  
    39.  
    40. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    41.  
    42.  
    43. QNetworkReply *reply = manager.post(request, sdata);
    44. currentUploads.append(reply);
    45. }
    46.  
    47. }
    48.  
    49. void uploadmanager::uploadFinished(QNetworkReply *reply)
    50. {
    51.  
    52. if (reply->error())
    53. {
    54. QByteArray response = reply->readAll();
    55. QFile file_spf("E:\\SMSAin\\dlog.txt");
    56. file_spf.open(QIODevice::WriteOnly | QIODevice::Text);
    57. QTextStream spf_in(&file_spf);
    58. spf_in<<response;
    59.  
    60. QString message_er = QString("Error in Upload");
    61. QMessageBox::information(this, "Err", message_er);
    62. }
    63. else
    64. {
    65. QString message_d = QString("Upload Complete");
    66. QMessageBox::information(this, "Upload Complete", message_d);
    67.  
    68. }
    69.  
    70. currentUploads.removeAll(reply);
    71. reply->deleteLater();
    72.  
    73. if (currentUploads.isEmpty())
    74. return;
    75.  
    76. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. youtube upload
    By ernie in forum Qt Programming
    Replies: 15
    Last Post: 2nd September 2015, 15:29
  2. FTP Upload using QNetworkAccessManager
    By replax in forum Newbie
    Replies: 2
    Last Post: 30th October 2014, 13:32
  3. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 22:56
  4. server upload
    By ag.sitesh in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 14:57
  5. Qhttp::request(...) method and GET request
    By mikhailt in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2006, 13:26

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
  •  
Qt is a trademark of The Qt Company.