Results 1 to 6 of 6

Thread: QHttp::post() - cannot upload file

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2008
    Location
    California
    Posts
    25
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question QHttp::post() - cannot upload file

    I'm trying to upload the file using Qhttp::post(). But the file on the other end goes in as null. I double checked to match exactly how the web browser posts it using http header plugin for Mozilla and both are sending the same data but the file is not being posted by my Qt app.

    here is my code:
    Qt Code:
    1. void Snipitron::connectS()
    2. {
    3. http = new QHttp(this);
    4. connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
    5. this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    6. connect(http, SIGNAL(readyRead(const QHttpResponseHeader&)),
    7. this, SLOT(readyRead(const QHttpResponseHeader&)));
    8.  
    9.  
    10. http->setHost("www.example.com");
    11.  
    12. QString initialPath = QDir::currentPath() + tr("/unt.") + "jpeg";
    13. userfile = new QFile(initialPath);
    14.  
    15. QByteArray data,payLoad;
    16. QString boundary="-----------------------------AaB03x";
    17. QString endline="\r\n";
    18. QString start_delim="--"+boundary+endline;
    19. QString cont_disp_str="Content-Disposition: form-data; ";
    20.  
    21. QString user_str = start_delim + cont_disp_str + "name=" + "\"user\""+endline+endline+"arun"+endline;
    22. data.append(QString(user_str).toUtf8());
    23.  
    24. QString pass_str = start_delim + cont_disp_str + "name=" + "\"pass\""+endline+endline+"1a27452283b0b46720913760f056377eb0b6388c"+endline;
    25. data.append(QString(pass_str).toUtf8());
    26.  
    27. QString proj_str = start_delim + cont_disp_str + "name=" + "\"project_name\""+endline+endline+"Misc"+endline;
    28. data.append(QString(proj_str).toUtf8());
    29.  
    30. QString projId_str = start_delim + cont_disp_str + "name=" + "\"project_id\""+endline+endline+"1951"+endline;
    31. data.append(QString(projId_str).toUtf8());
    32.  
    33. QString notes_str = start_delim + cont_disp_str + "name=" + "\"title\""+endline+endline+"Test project add - Qt way"+endline;
    34. data.append(QString(notes_str).toUtf8());
    35.  
    36. if (!userfile->open(QIODevice::ReadOnly))
    37. QMessageBox::critical(this, "Arun", "File not found", QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
    38.  
    39.  
    40. QString title_str = start_delim + cont_disp_str + "name=" + "\"uploadfile1\""+ ";filename=unt.jpeg" +endline + "Content-Type: image/jpeg" +endline+endline;
    41. title_str.append(userfile->readAll());
    42. title_str = title_str + endline;
    43. data.append(QString(title_str).toUtf8());
    44. userfile->close(); // the file is opened earlier in the code
    45.  
    46. QString v_str = start_delim + cont_disp_str + "name=" + "\"v\""+endline+endline+"1.0"+endline;
    47. //data.writeRawBytes(v_str.data(),v_str.length());
    48. data.append(QString(v_str).toUtf8());
    49.  
    50. QString cmd_str = start_delim + cont_disp_str + "name=" + "\"command\""+endline+endline+"add"+endline;
    51. //data.writeRawBytes(cmd_str.data(),cmd_str.length());
    52. data.append(QString(cmd_str).toUtf8());
    53.  
    54. QString stop_delim="--"+boundary+"--"+endline;
    55. data.append(QString(stop_delim).toUtf8());
    56.  
    57. QHttpRequestHeader header("POST", "/app/submit.jsp");
    58. header.setValue("Host", "www.example.com");
    59. header.setContentType("multipart/form-data, boundary=-----------------------------AaB03x");
    60. header.setContentLength(data.length());
    61. QMessageBox::critical(this, "Arun", data, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
    62. http->request(header, data);
    63. }
    To copy to clipboard, switch view to plain text mode 

    what am i doing wrong here???

    Thanks,
    Last edited by jpn; 16th May 2008 at 07:40. Reason: disabled smilies
    - AR

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. 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
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. 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
  5. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 22:02

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.