473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading binary data from web site

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

Nov 16 '05 #1
2 5580
Pete Davis <pd******@NOSPAM.hotmail.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Pete Davis <pd******@NOSPAM.hotmail.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String vstr = rksub.GetValue(vs).ToString(); OR String...
23
by: Graham F French | last post by:
Hello, I can read text files into my application, but I cannot read in msg files as they seem to be in a proprietry format. Is there anyway of converting it on the fly or is there an...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
by: John Wright | last post by:
I am trying to read the data from a device on a serial port. I connect just fine and can receive data fine in text mode but not in binary mode. In text mode the data from the device comes in...
13
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.