//first define the parameters: host, sub Url, <parameter> for file to be uploaded and <parameter> for filename and <
QString upUrl
("/cgi-bin/run/~jkorpela/echo.cgi");
QString upFileDescription
("QtTest");
//pre load the file to be sent, for huge files one would not load it here but give an open file aka QIODevice to the request.
QString exportFName
("/Users/dev/eportFile.bin");
QFile exportFile
(exportFName
);
return;
exportFile.close();
//file part
QString boundary
= "---------------------------193971182219750";
datas
+= "Content-Disposition: form-data; name=\""+upFileName
+"\"; filename=\""+QFileInfo(exportFName
).
fileName()+"\"\r\n";
datas += "Content-Type: application/octet-stream\r\n\r\n";
datas += fContents;
datas += "\r\n";
//description part
datas
+= QString("--" + boundary
+ "\r\n").
toAscii();
datas += "Content-Disposition: form-data; name=\""+upFileText+"\"\r\n\r\n";
datas += upFileDescription+"\r\n";
datas
+= QString("--" + boundary
+ "--\r\n").
toAscii();
header.setValue("Host", host);
header.setValue("Content-Type", "multipart/form-data; boundary=" + boundary);
header.
setValue("Content-Length",
QString::number(datas.
length()));
http->setHost(host);
QEventLoop loop;
// wait & block for transfer to finish for longer files may show an animation or find out how to display progress... connect(http, SIGNAL(done(bool)), &loop, SLOT(quit()));
connect(http, SIGNAL(dataSendProgress(int,int)), this, SLOT(slotDataSendProgress(int, int))); //optional progress proc
//actually sending & receiving stuff
http->request(header, datas, &responseBuf);
loop.exec();
//checking for error and displaying result if successful
if(http
->error
() != QHttp::NoError) resultMsg = tr("An error occured while trying to upload the file: \n")+http->errorString();
else
resultMsg = tr("Successfully transfered file, the response was:\n")+response;
QMessageBox::information(this, tr
("web transfer result"), resultMsg
);
delete http;
...
//optional progress slot:
void MyStoopidClass::slotDataSendProgress(int done, int total)
{
qDebug() << done << " of " << total;
}
//first define the parameters: host, sub Url, <parameter> for file to be uploaded and <parameter> for filename and <
QString host("www.cs.tut.fi");
QString upUrl("/cgi-bin/run/~jkorpela/echo.cgi");
QString upFileName("datafile");
QString upFileText("textline");
QString upFileDescription("QtTest");
QByteArray response;
QBuffer responseBuf(&response);
//pre load the file to be sent, for huge files one would not load it here but give an open file aka QIODevice to the request.
QString exportFName("/Users/dev/eportFile.bin");
QFile exportFile(exportFName);
if(!exportFile.open(QIODevice::ReadOnly))
return;
QByteArray fContents = exportFile.readAll();
exportFile.close();
//file part
QHttp *http = new QHttp(this);
QString boundary = "---------------------------193971182219750";
QByteArray datas(QString("--" + boundary + "\r\n").toAscii());
datas += "Content-Disposition: form-data; name=\""+upFileName+"\"; filename=\""+QFileInfo(exportFName).fileName()+"\"\r\n";
datas += "Content-Type: application/octet-stream\r\n\r\n";
datas += fContents;
datas += "\r\n";
//description part
datas += QString("--" + boundary + "\r\n").toAscii();
datas += "Content-Disposition: form-data; name=\""+upFileText+"\"\r\n\r\n";
datas += upFileDescription+"\r\n";
datas += QString("--" + boundary + "--\r\n").toAscii();
QHttpRequestHeader header("POST", upUrl);
header.setValue("Host", host);
header.setValue("Content-Type", "multipart/form-data; boundary=" + boundary);
header.setValue("Content-Length", QString::number(datas.length()));
http->setHost(host);
QEventLoop loop; // wait & block for transfer to finish for longer files may show an animation or find out how to display progress...
connect(http, SIGNAL(done(bool)), &loop, SLOT(quit()));
connect(http, SIGNAL(dataSendProgress(int,int)), this, SLOT(slotDataSendProgress(int, int))); //optional progress proc
//actually sending & receiving stuff
http->request(header, datas, &responseBuf);
loop.exec();
//checking for error and displaying result if successful
QString resultMsg;
if(http->error() != QHttp::NoError)
resultMsg = tr("An error occured while trying to upload the file: \n")+http->errorString();
else
resultMsg = tr("Successfully transfered file, the response was:\n")+response;
QMessageBox::information(this, tr("web transfer result"), resultMsg);
delete http;
...
//optional progress slot:
void MyStoopidClass::slotDataSendProgress(int done, int total)
{
qDebug() << done << " of " << total;
}
To copy to clipboard, switch view to plain text mode
Bookmarks