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



  • you will see from docs that readAll() has no mechanism to return error on failure and returns an empty bytearray. maybe you need to use some other call ...



  • 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();@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.