Hi,
Well, does someone has an example of file upload with
QNetworkRequest and QNetworkAccessManager.
Qt is really laking documentation on that...
Best regards,
Oscar
Hi,
Well, does someone has an example of file upload with
QNetworkRequest and QNetworkAccessManager.
Qt is really laking documentation on that...
Best regards,
Oscar
Here is one of the tries I did for the upload...
It doesn't work with the Qt code.
However, with a html forms, the PHP script get the uploaded file correctly...
Qt Code:
QNetworkAccessManager *manager; manager = new QNetworkAccessManager(); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinishedSlot(QNetworkReply*))); QNetworkRequest req; req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data, Boundary: -----------------1111553253537452673632\n"); req.setHeader(QNetworkRequest::ContentLengthHeader, 4); manager->post(req, "-----------------1111553253537452673632\nabcd");To copy to clipboard, switch view to plain text mode
Best regards,
Oscar
I think at least content-length is wrong. It should contain all you send as the contents of POST. Currently the server probably only interprets "----" as these are the four first bytes of the content.
Thank you wysota for your help,
I tried a length of 43 characters (39 characters of header + 4 characters of datas), but It doesn't work better...
Qt Code:
QNetworkRequest req; req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=-----------------1111553253537452673632"); //39 req.setHeader(QNetworkRequest::ContentLengthHeader, 43); manager->post(req, "-----------------1111553253537452673632abcd");To copy to clipboard, switch view to plain text mode
He is the remote PHP script:
Qt Code:
<? $fhLog = fopen("log.txt",'a'); foreach ($_FILES as $myfile) { fwrite($fhLog, $myfile['tmp_name']); } fclose($fhLog); ?>To copy to clipboard, switch view to plain text mode
As I said, when using a basic upload form, the PHP script is getting correctly the uploaded file, but with QT, the PHP script is called well, I can send HTTP headers fields but I don't find a way to upload files...
Sending just POST fields with QT to a PHP script also works (I then get the fields in the PHP using $_POST['myField]). The only problem is with uploaded files... Grrrr...
I should do a wireshark capture to see the QT request content but It's not really easy...
There must be some examples on the web, but I can't find anyone...
I hope someone can give me some more help.
Thanks.
Oscar
Last edited by oscar; 14th December 2008 at 09:11.
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:
<form method="post" enctype="multipart/form-data" action="upload.php"> <p> <input type="file" name="fichier" size="30"> <input type="submit" name="upload" value="Uploader"> </p> </form>To copy to clipboard, switch view to plain text mode
And here is the Qt code :
Qt Code:
datas += "Content-Disposition: form-data; name=\"fichier\"; filename=\"DSCF1055.jpg\"\r\n"; datas += "Content-Type: image/jpeg\r\n\r\n"; return; datas += file.readAll(); datas += "\r\n"; datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n"; datas += "Uploader\r\n"; header.setValue("Host", "www.myhost.com"); header.setValue("Content-Type", "multipart/form-data; boundary=" + boundary); http->setHost("www.myhost.com"); 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 "--"
You can find one solution for poting to web forms (with file uploads) here: http://www.tuckdesign.com/sources/Qt
Bookmarks