Upload picture with Qt
-
hello,
I want to upload a picture with Qt and send it to a script php that exist in server
i try this example but it doesn-t work:
@
void upload::uploadImage()
{//path image QString path("C:/QtProjets/uploadImage/test.png"); //php script that receives the image QNetworkRequest requete(QUrl("http://link server/upload.php")); //our server with php-script QByteArray boundary = "-------------------------87142694621188"; QFile file(path); if (!file.open(QIODevice::ReadOnly)) { qDebug()<<"erreur read image"; return; } QByteArray fileContent(file.readAll()); QByteArray data = "--" + boundary + "\r\n"; data += "Content-Disposition: form-data; name=\"data\"; filename=\"test.png\";\r\n"; //add picture to data data += "Content-Type: image/png\r\n\r\n" + fileContent + "\r\n"; data += "--" + boundary + "--\r\n"; requete.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary); requete.setRawHeader("Content-Length", QString::number(data.size()).toAscii()); file.close(); qDebug() <<"data"<< data.size(); QNetworkAccessManager *am = new QNetworkAccessManager(this); QNetworkReply *reply = am->post(requete,"data="+data); QObject::connect(am, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}
/************************************ response /
/***********************************************************************/void upload::replyFinished(QNetworkReply *reply)
{
reply->open(QIODevice::ReadOnly);// if the response is correct if(reply->error() == QNetworkReply::NoError) { QByteArray str=(reply->readAll()); QString response = QString::fromUtf8(str.data(), str.size()); qDebug()<<" re "<<response; } //error sever else qDebug()<<"error response server";
}
@and my code php ( upload.php):
@<?php$data =$_POST['data'];
$fp = fopen("image.png", 'w+');
fputs($fp, $data);
fclose($fp);
echo 'hello'?>
@
i think the problem is in this lines 21 ans 22:
@
data += "Content-Type: image/jpeg\r\n\r\n" + fileContent + "\r\n";
data += "--" + boundary + "--\r\n";@
Any help and advice would be much appreciated! -