Reading binary data from web site

  • Thread starter Thread starter Pete Davis
  • Start date Start date
P

Pete Davis

I'm trying to read some image files off of our web site and the code isn't
working. The files appear to be corrupt. The file size is correct the image
becomes corrupt after maybe 20 or 30 lines.

The url might be something like:

http://www.website.com/folder/chart1.jpg

And I want to save this into a file called chart1.jpg in a folder I've
specified. The code is:

WebRequest req = WebRequest.Create(url);
using (WebResponse resp = req.GetResponse())
{
using(Stream stream = resp.GetResponseStream())
{
string outFile = _destinationTextBox.Text + @"\" + filename;
FileStream fs = new FileStream(outFile, FileMode.Create);
byte[] buffer = new byte[resp.ContentLength];
stream.Read(buffer, 0, (int) resp.ContentLength);
fs.Write(buffer, 0, (int) resp.ContentLength);
fs.Close();
}
}

What am I missing or where is the bug I'm overlooking?

Thanks.

Pete
 
Pete Davis said:
I'm trying to read some image files off of our web site and the code isn't
working. The files appear to be corrupt. The file size is correct the image
becomes corrupt after maybe 20 or 30 lines.

The url might be something like:

http://www.website.com/folder/chart1.jpg

And I want to save this into a file called chart1.jpg in a folder I've
specified. The code is:

WebRequest req = WebRequest.Create(url);
using (WebResponse resp = req.GetResponse())
{
using(Stream stream = resp.GetResponseStream())
{
string outFile = _destinationTextBox.Text + @"\" + filename;
FileStream fs = new FileStream(outFile, FileMode.Create);
byte[] buffer = new byte[resp.ContentLength];
stream.Read(buffer, 0, (int) resp.ContentLength);
fs.Write(buffer, 0, (int) resp.ContentLength);
fs.Close();
}
}

What am I missing or where is the bug I'm overlooking?

You're ignoring the return value of Stream.Read, and assuming that
every time you call it it's completely filling the buffer you've
provided.

See http://www.pobox.com/~skeet/csharp/readbinary.html
 
You know, I was looking at it thinking, surely there's some way to loop
through this if it's not returning all the data, but I just couldn't put my
finger on how. I was looking at the response object.

Thanks Jon.

Pete

Jon Skeet said:
Pete Davis said:
I'm trying to read some image files off of our web site and the code isn't
working. The files appear to be corrupt. The file size is correct the image
becomes corrupt after maybe 20 or 30 lines.

The url might be something like:

http://www.website.com/folder/chart1.jpg

And I want to save this into a file called chart1.jpg in a folder I've
specified. The code is:

WebRequest req = WebRequest.Create(url);
using (WebResponse resp = req.GetResponse())
{
using(Stream stream = resp.GetResponseStream())
{
string outFile = _destinationTextBox.Text + @"\" + filename;
FileStream fs = new FileStream(outFile, FileMode.Create);
byte[] buffer = new byte[resp.ContentLength];
stream.Read(buffer, 0, (int) resp.ContentLength);
fs.Write(buffer, 0, (int) resp.ContentLength);
fs.Close();
}
}

What am I missing or where is the bug I'm overlooking?

You're ignoring the return value of Stream.Read, and assuming that
every time you call it it's completely filling the buffer you've
provided.

See http://www.pobox.com/~skeet/csharp/readbinary.html
 
Back
Top