Results 1 to 13 of 13

Thread: Upload request

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

    Default Upload request

    I am trying to upload a file on the server. I have written this code, but i am receiving error in reply from server (on call of uploadfinished() [in code])

    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. foreach (QString arg, args) {
    13. QFile file(arg);
    14. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    15. {
    16. QMessageBox::information(this, "Error", message_d);
    17. return;
    18. }
    19.  
    20. QNetworkRequest request;
    21. request.setUrl(QUrl("me.xyz.com/smsspam"));
    22. request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
    23. request.setRawHeader("Accept","text/plain");
    24. file.open(QIODevice::ReadOnly | QIODevice::Text);
    25. QByteArray fileBinaryData(file.readAll());
    26. file.close();
    27. request.setRawHeader("data",fileBinaryData);
    28. QNetworkReply *reply = manager.post(request, fileBinaryData);
    29. currentUploads.append(reply);
    30. }
    31.  
    32. }
    33.  
    34. void uploadmanager::uploadFinished(QNetworkReply *reply)
    35. {
    36.  
    37. if (reply->error())
    38. {
    39. QString message_er = QString("Error in Upload");
    40. QMessageBox::information(this, "Err", message_er); //I am getting this msg
    41. }
    42. else
    43. {
    44. QString message_d = QString("Upload Complete");
    45. QMessageBox::information(this, "Upload Complete", message_d);
    46.  
    47. }
    48.  
    49. currentUploads.removeAll(reply);
    50. reply->deleteLater();
    51.  
    52. if (currentUploads.isEmpty())
    53. return;
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 


    The PHP script at the server is below:

    Qt Code:
    1. <?php
    2. // Read the raw http data from the post
    3. $data = $_POST['data'];
    4. // Ungzip the wav file
    5. // $chunk = gzuncompress($data);
    6. if (!$data) {
    7. echo -1;
    8. die();
    9. }
    10. $uid = uniqid();
    11. $filename = "$uid.csv";
    12. $filepathname = "uploads/$filename";
    13. $handle = fopen($filepathname, 'wb');
    14. fputs($handle, $data, strlen($data));
    15. fclose($handle);
    16. ?>
    To copy to clipboard, switch view to plain text mode 

    Kindly, help me in constructing packet header and request in correct format as per the script. I think, i am not doing that in right way.

    Any help would be appreciated.

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

    Default Re: Upload request

    You are correct in that you can not upload to the server as you have done. You need to upload in a format suitable for the server.

    You may wish to research MIME encoding.

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

    dipeshtech (4th May 2011)

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

    Default Re: Upload request

    Thanks squidge..!! But, still i am unable to figure out the right syntax by which i can include "data" in my request. Can i get some hint regarding that. I would be happy to know, if anyone can suggest me the wrong (in the context what i am trying to achieve) things which i have written in my code.

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

    Default Re: Upload request

    The data has to be in the body part of the request and not in the request header. If you are using x-www-form-url encoding then first learn what the encoding looks like before trying to use it.
    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.


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

    dipeshtech (4th May 2011)

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

    Default Re: Upload request

    Thanks wysota!

    But, I don't know how to include 'data' in body part. Actually, i don't how to include that in body part. I too had doubt on that point that i am doing wrong in including it in header. Help me please, i don't know how to do that in qt.

    And, you are right there! I will research on understanding the form-url-encoding thing.

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

    Default Re: Upload request

    Quote Originally Posted by dipeshtech View Post
    Thanks wysota!

    But, I don't know how to include 'data' in body part. Actually, i don't how to include that in body part. I too had doubt on that point that i am doing wrong in including it in header. Help me please, i don't know how to do that in qt.
    Read the docs for QNetworkAccessManager::post(). And use your head instead of blindly trying some random things.
    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.


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

    Default Re: Upload request

    First of all i am not an expert and still very naive. I have read the docs of QNetworkAccessManager:: post() and i have used it in my code and also used MY HEAD [see above]. My question is how to identify the data of file with attribute 'data' [check the server side PHP script]. If you can answer that is fine for me.

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

    Default Re: Upload request

    Quote Originally Posted by dipeshtech View Post
    If you can answer that is fine for me.
    You have already been answered that:

    Quote Originally Posted by squidge
    You need to upload in a format suitable for the server.
    And you even know how to do that:
    Quote Originally Posted by dipeshtech
    I will research on understanding the form-url-encoding thing.
    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.


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

    Default Re: Upload request

    I said "I will" . So, for now i don't know how to do that. That's why, urging you for help.

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

    Default Re: Upload request

    Quote Originally Posted by dipeshtech View Post
    I said "I will".
    So do that.

    So, for now i don't know how to do that. That's why, urging you for help.
    http://www.lmgtfy.com?q=x-www-form-urlencoded
    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.


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

    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".

  14. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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.

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

    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, 14:29
  2. FTP Upload using QNetworkAccessManager
    By replax in forum Newbie
    Replies: 2
    Last Post: 30th October 2014, 12:32
  3. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 21:56
  4. server upload
    By ag.sitesh in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 13:57
  5. Qhttp::request(...) method and GET request
    By mikhailt in forum Qt Programming
    Replies: 4
    Last Post: 15th September 2006, 12: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.