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>
<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 :
QString boundary
= "---------------------------193971182219750";
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");
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();
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);
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);
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 "--"
Bookmarks