I am writing an image upload widget that sends the data to a PHP script and gets the new uploaded filename as http response from the PHP script.
@jdi, that was one problem now i get "? as the response with qDebug() << rep->readAll(), so the first readAll clears the buffer.
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
image->save(&buffer, "JPG");
QNetworkAccessManager *http=new QNetworkAccessManager(this);
QNetworkRequest r(QUrl("http://domain/test.php"));
QString bound="---------------------------723690991551375881941828858";
QByteArray data(QString("--"+bound+"\r\n").toAscii());
data += "Content-Disposition: form-data; name=\"action\"\r\n\r\n";
data += "\r\n";
data += QString("--" + bound + "\r\n").toAscii();
data += "Content-Disposition: form-data; name=\"file\"; filename=\"test.jpg\"\r\n";
data += "Content-Type: image/JPG\r\n\r\n";
data += bytes;
data += "\r\n";
data += QString("--" + bound + "\r\n").toAscii();
data += QString("--" + bound + "\r\n").toAscii();
data += "Content-Disposition: form-data; name=\"desc\"\r\n\r\n";
data += "Description for my image here :)\r\n";
data += "\r\n";
r.setRawHeader(QString("Accept-Encoding").toAscii(), QString("gzip,deflate").toAscii());
r.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
r.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());
rep = http->post(r,data);
connect(http,SIGNAL(finished(QNetworkReply*)),this,SLOT(uploadFinished(QNetworkReply*)));
And the callback function:
void UploadWidget::uploadFinished(QNetworkReply* r)
{
r->deleteLater();
if(r->error() == QNetworkReply::NoError) {
QByteArray data = r->readAll();
//QString s = r->readAll(); solved - readAll clears the buffer :)
}
}
i iterated with QByteArray b = rep->readAll(); for (int i = 0 ; i< b.size(); i++) { qDebug() << (char)b.at(i); } is there any way to convert the whole bytearray to Qstring because i get only some symbols
? 3 , É E , V c D ? ? Ô â C ö f § should be "1this is a test1"
Apparently from your comments your problem was that you were reading all of the response object more than once. A response object is a sequential IO object so once you read it, it is gone.
QNetworkReply is a sequential-access QIODevice, which means that once data is read from the object, it no longer kept by the device.
Read it once to get the QByteArray. You can convert it to a QString.
QByteArray data = r->readAll();
QString dataStr(data);
qDebug() << dataStr;
Another thing to note is that you should not be creating a brand new QNetworkAccessManager in every upload call. You are leaking it each time, and almost missing out on the fact that keeping one around for the entire application automatically queues multiple requests for you. Create one for your entire application and share it.
Also, you might want to look into using the QHttpMultipart
version of the post
method, as it seems to be a much cleaner way for you to set all of the headers. You can almost copy the given method, which is for posting a jpeg:
QHttpMultipart JPEG post example
The actual cause of your problem reading the data is this header:
r.setRawHeader(QString("Accept-Encoding").toAscii(),
QString("gzip,deflate").toAscii());
It is telling the server to send back the results as gzip compressed. What you end up reading would have to be decompressed first. Just remove that header and you should receive your plain text test response.
receiver.h
#include <QObject>
class QNetworkReply;
class Receiver : public QObject
{
Q_OBJECT
public:
explicit Receiver(QObject* parent = 0);
public slots:
void finished(QNetworkReply*);
};
main.cpp
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDebug>
#include "receiver.h"
Receiver::Receiver(QObject *parent)
: QObject(parent)
{}
void Receiver::finished(QNetworkReply *reply) {
reply->deleteLater();
QByteArray data = reply->readAll();
qDebug() << data;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QNetworkAccessManager net;
Receiver rec;
net.connect(&net, SIGNAL(finished(QNetworkReply*)),
&rec, SLOT(finished(QNetworkReply*)));
QNetworkRequest r(QUrl("http://poki.ba/test.html"));
r.setHeader(r.ContentTypeHeader, QVariant("multipart/form-data;"));
r.setHeader(r.ContentLengthHeader, QVariant("100"));
// r.setRawHeader(QString("Accept-Encoding").toAscii(),
// QString("gzip,deflate").toAscii());
net.post(r, QByteArray());
return a.exec();
}