PDA

View Full Version : file upload using QNetworkRequest & QNetworkAccessManager



oscar
13th December 2008, 23:24
Hi,

Well, does someone has an example of file upload with
QNetworkRequest and QNetworkAccessManager.

Qt is really laking documentation on that...


Best regards,
Oscar

oscar
14th December 2008, 00:07
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...


QNetworkAccessManager *manager;
manager = new QNetworkAccessManager();
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinishedSlot(QNetworkReply*)));
QNetworkRequest req;
req.setUrl(QUrl("http://mywebsite.com/upload.php"));
req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data, Boundary: -----------------1111553253537452673632\n");
req.setHeader(QNetworkRequest::ContentLengthHeader , 4);
manager->post(req, "-----------------1111553253537452673632\nabcd");


Best regards,
Oscar

wysota
14th December 2008, 02:27
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.

oscar
14th December 2008, 09:45
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...


QNetworkRequest req;
req.setUrl(QUrl("https://mywebsite.com/upload.php"));
req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=-----------------1111553253537452673632"); //39
req.setHeader(QNetworkRequest::ContentLengthHeader , 43);
manager->post(req, "-----------------1111553253537452673632abcd");


He is the remote PHP script:


<?
$fhLog = fopen("log.txt",'a');
foreach ($_FILES as $myfile) {
fwrite($fhLog, $myfile['tmp_name']);
}
fclose($fhLog);
?>


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

nooky59
26th December 2008, 17:13
HTTP upload works like a charm !

Here is a quick example with the upload of a JPEG image.

It matches such an HTML form



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


And here is the Qt code :



QHttp *http = new QHttp(this);
QString boundary = "---------------------------193971182219750";

QByteArray datas(QString("--" + boundary + "\r\n").toAscii());
datas += "Content-Disposition: form-data; name=\"fichier\"; filename=\"DSCF1055.jpg\"\r\n";
datas += "Content-Type: image/jpeg\r\n\r\n";

QFile file("C:\\DSCF1055.jpg");
if (!file.open(QIODevice::ReadOnly))
return;

datas += file.readAll();
datas += "\r\n";
datas += QString("--" + boundary + "\r\n").toAscii();
datas += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
datas += "Uploader\r\n";
datas += QString("--" + boundary + "--\r\n").toAscii();

QHttpRequestHeader header("POST", "/upload.php");
header.setValue("Host", "www.myhost.com");
header.setValue("Content-Type", "multipart/form-data; boundary=" + boundary);
header.setValue("Content-Length", QString::number(datas.length()));

http->setHost("www.myhost.com");
http->request(header, datas);


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

krunoslav
20th January 2009, 15:11
You can find one solution for poting to web forms (with file uploads) here: http://www.tuckdesign.com/sources/Qt