01-17-2013 05:21 AM
I have tried the following code for image uploading,in this code am able to get the response from server but the image size is always zero in server.Can anyone help me about this regards,
void App::uploadImage(){
QString path(QDir::currentPath() + "/app/native/assets/Icon/img.jpg");
QNetworkRequest request(
QUrl( "http://domain.com?type=uploadimages&deviceid=12323"));
QByteArray boundary = "-------------------------32142544626177";
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "error read image";
return;
}
QByteArray fileContent(file.readAll());
QByteArray data = "--" + boundary + "\r\n";
data +=
"Content-Disposition: form-data; name=\"data\"; filename=\"img.jpg\";\r\n";
data += "Content-Type: image/jpg\r\n\r\n" + fileContent + "\r\n";
data += "--" + boundary + "--\r\n";
request.setRawHeader("Content-Type",
"multipart/form-data; boundary=" + boundary);
request.setRawHeader("Content-Length",
QString::number(data.size()).toAscii());
file.close();
qDebug() << "data" << data.size();
QNetworkAccessManager *am = new QNetworkAccessManager(this);
QNetworkReply *reply = am->post(request, "&data=" +data);
QObject::connect(am, SIGNAL(finished(QNetworkReply*)), this,
SLOT(replyFinished(QNetworkReply*)));
}
void App::replyFinished(QNetworkReply* reply) {
reply->open(QIODevice::ReadOnly);
if (reply->error() == QNetworkReply::NoError) {
QByteArray str = (reply->readAll());
QString response = QString::fromUtf8(str.data(), str.size());
qDebug() << " response " << response;
}
else{
qDebug() << "error response server";
}
}
01-17-2013 08:04 AM
04-09-2013 08:25 AM
There is a good example on how to implement a post request.
QFile fileUp("FILE PATH");
fileUp.open(QIODevice::ReadOnly);
QByteArray file(fileUp.readAll());
fileUp.close();
QByteArray boundary("AaB03x");
QNetworkRequest req(QUrl("https://evsweb39.idrive.com/evs/uploadFile"));
req.setRawHeader("Content-Type", QByteArray("multipart/form-data; charset=UTF-8; boundary=") + boundary);
QByteArray postData;
postData += "--" + boundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"uid\"\r\n\r\n";
postData += userName + "\r\n";
postData += "--" + boundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"pwd\"\r\n\r\n";
postData += passWord + "\r\n";
postData += "--" + boundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"p\"\r\n\r\n";
postData += remotePath + "\r\n";
postData += "--" + boundary + "\r\n";
postData += "Content-Disposition: form-data; filename=\"" + fileName + "\";\r\nContent-Type: application/octet-stream\r\n\r\n";
postData += file;
postData += "\r\n";
postData += "--" + boundary + "--\r\n";
req.setHeader(QNetworkRequest::ContentLengthHeader , QString::number(postData.length()));
This example is part of another forum thread.
http://www.qtcentre.org/threads/48475-POST-Request
04-09-2013 01:18 PM
okay thanks..Can you give me the php server side code also?
04-11-2013 01:20 PM
Finally i have tried the below link and it works fine..
http://www.qtforum.org/article/31597/problems-with