How to upload a break off file using QHttpMultiPart?
-
I can upload files with QHttpMultiPart,like sample in qt creator:
@
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="text""));
textPart.setBody("my text");QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="image""));
QFile *file = new QFile("image.jpg");
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPartmultiPart->append(textPart);
multiPart->append(imagePart);QUrl url("http://my.server.tld");
QNetworkRequest request(url);QNetworkAccessManager manager;
QNetworkReply *reply = manager.post(request, multiPart);
multiPart->setParent(reply); // delete the multiPart with the reply
// here connect signals etc.
@And now, my question is how to upload a break off file.
With same code, just call QFile.seek() before upload the break off file,
the code cann't work, actually, the code cann't exit.
code:
@
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="text""));
textPart.setBody("my text");QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="image""));
QFile *file = new QFile("image.jpg");
file->seek(1000);
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPartmultiPart->append(textPart);
multiPart->append(imagePart);QUrl url("http://my.server.tld");
QNetworkRequest request(url);QNetworkAccessManager manager;
QNetworkReply *reply = manager.post(request, multiPart);
multiPart->setParent(reply); // delete the multiPart with the reply
// here connect signals etc.QTimer timer;
QEventLoop loop;
QObject::connect(&timer,SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(100);int updateProgressTimes = 0;
while(!pReply->isFinished())
{
loop.exec(); // in the last, this exec cann't exit, and cpu is 100%
}
@Any advice?
Thanks.[edit: added missing coding tags @ SGaist]