0

I'm using the following code to download an image:

Dim strImageURL as string = "http://images.bookworld.com.au/images/bau/97817433/9781743315187/0/0/plain/the-storyteller.jpg"
Dim data As Byte()
Using client As New WebClient()
  data = client.DownloadData(strImageURL)
  'client.DownloadFile(strImageURL, "MyFile.jpg")
End Using
File.WriteAllBytes("\\mappedPath\Images\MyFile2.jpg", data)

The code works fine for most cases, but we've come across an image that once saved, is corrupted..

This is the troubling URL: http://images.bookworld.com.au/images/bau/97817433/9781743315187/0/0/plain/the-storyteller.jpg

I tried using the code above and then the "client.DownloadFile" line thinking that they may have different results, but I keep getting a corrupted file.

I am able to hit the URL and Chrome displays the image fine.. I can save the file to my local machine fine too.. but using the above code, I just get corrupted .jpg files for this particular URL..

I suspect the image is on some kind of image serving service and possibly not sending something quite correctly.. but I'm not sure..

I even tried the same code in C# to see what it would do, but I got the same result.. :-(

If anyone can help sort this out, I'd be very grateful..

Thanks.

Ads
  • 2,084
  • 2
  • 24
  • 34
  • Check that http://stackoverflow.com/questions/3615800/download-image-from-the-site-in-net-c – Mate Mar 04 '13 at 02:29

1 Answers1

3

Looking at the response headers from the web server for that particular URL reveals Content-Encoding:gzip. In other words, it's not returning a jpg, it's returning a gzip. Web browsers are clever enough to automatically decode the zip, but WebClient is not. Note that gzip is not your standard .zip or "PK zip", so don't try naming the file as a .zip and expect it to unzip.

It's possible to get WebClient to automatically decode a gzip response, but requires a few tricks - Uncompressing gzip response from WebClient

Community
  • 1
  • 1
Snixtor
  • 4,239
  • 2
  • 31
  • 54
  • That was so simple... Thanks very much.. I saw that the file was zipped but didn't even think that would be the issue.. – Ads Mar 04 '13 at 05:04