Downloading Image File
-
I am trying to download a jpg file...
@nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpCompleted(QNetworkReply*)));QUrl url("http://mysite.com/test.jpg");
nam->get(QNetworkRequest(url));@In httpCompleted...
@if (reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll();
int count = bytes.count();... save to file
}@
This mechanism works for non-image files but fails for images.
bytes.count() is zero even though reply->bytesAvailable() is the size of the image file.Anyone seen this before?
Thanks
Simon
-
Using read() instead returns minus 1. reply->errorString(); is "Unknown error".
Simon
-
It's possible that no error is returned if a redirection occurred. Try to check content of bytes (qDebug() << bytes) or try to look for a redirection:
@QString forwardedUrl;
forwardedUrl= reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString();@