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