PDA

View Full Version : QT5 http post request



marty.marty
14th January 2015, 11:44
Hi everyone,

I'm trying to upload a file using ost request and a PHP script but i'm having some problems.
My file isn't uploaded to the server ($_FILES always empty even if my php script is working, validated with an HTML form).
Bytes seem to be transfered but nothing in $_FILES.

I spent a lot of time wondering why and trying all examples given on the web, so if someone can help me...

Here is my code :


QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("form-data; name=\"text\""));
textPart.setBody("my text");

QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHe ader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispos itionHeader, QVariant("form-data; name=\"image\""));
QFile *file = new QFile("C:/..../test.jpg");
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart

multiPart->append(textPart);
multiPart->append(imagePart);

QUrl url("http://..../test.php");
QNetworkRequest request(url);

QNetworkAccessManager * manager = new QNetworkAccessManager();
reply = manager->post(request, multiPart);
multiPart->setParent(reply); // delete the multiPart with the reply


connect(reply, SIGNAL( uploadProgress(qint64, qint64) ), this, SLOT( uploadProgress(qint64,qint64) ) ) ;

connect(reply, SIGNAL(finished()), this, SLOT(uploadDone()));



void uploadProgress(qint64 bytesSent, qint64 bytesTotal) {
qDebug() << "Uploaded" << bytesSent << "of" << bytesTotal;
}

void uploadDone() {
qDebug() << "Finished" << reply->errorString() <<reply->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug()<<reply->readAll();

//reply->deleteLater();
}

And my ouput :


Uploaded 16384 of 66201
Uploaded 66201 of 66201
Uploaded 0 of 0
Finished "Unknown error" 200
"$_FILES: Array
(
)

Maybe the "Uploaded 0 of 0"...

Any help would be appreciated!

Thanking you in advance

Marty