February 15, 2011

SimonJudge SimonJudge
Lab Rat
70 posts

Downloading Image File

 

I am trying to download a jpg file…

  1. nam = new QNetworkAccessManager(this);
  2. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpCompleted(QNetworkReply*)));
  3.  
  4. QUrl url("http://mysite.com/test.jpg");
  5. nam->get(QNetworkRequest(url));

In httpCompleted…

  1. if (reply->error() == QNetworkReply::NoError)
  2. {
  3. QByteArray bytes = reply->readAll();
  4. int count = bytes.count();
  5.  
  6. ... save to file
  7.  
  8. }

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

3 replies

February 15, 2011

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

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 …

February 15, 2011

SimonJudge SimonJudge
Lab Rat
70 posts

Using read() instead returns minus 1. reply->errorString(); is “Unknown error”.

Simon

February 15, 2011

marcoB marcoB
Lab Rat
167 posts

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:

  1. QString forwardedUrl;
  2. forwardedUrl= reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString();

 
  ‹‹ [Solved]QString input using argv      How to get DDE data? ››

You must log in to post a reply. Not a member yet? Register here!