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
::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\""));
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()));
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("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()));
To copy to clipboard, switch view to plain text mode
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
And my ouput :
Uploaded 16384 of 66201
Uploaded 66201 of 66201
Uploaded 0 of 0
Finished "Unknown error" 200
"$_FILES: Array
(
)
Uploaded 16384 of 66201
Uploaded 66201 of 66201
Uploaded 0 of 0
Finished "Unknown error" 200
"$_FILES: Array
(
)
To copy to clipboard, switch view to plain text mode
Maybe the "Uploaded 0 of 0"...
Any help would be appreciated!
Thanking you in advance
Marty
Bookmarks