Important: Please read the Qt Code of Conduct -

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("");

    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?



  • 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".


  • 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