does anyone see what is wrong with my .gif download logic? no matter what image url i try it downloa

D

dr

does anyone see what is wrong with my .gif download logic? no matter what
image url i try it downloads a junk file that can't be opened in any paint
program.


System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url);
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream imgStream = myResponse.GetResponseStream();
long len = myResponse.ContentLength;
byte[] binarydata = new byte[len];
System.IO.Stream streambinary = myResponse.GetResponseStream();
streambinary.Read(binarydata, 0, (int)len);
using(System.IO.BinaryWriter binWriter = new
System.IO.BinaryWriter(System.IO.File.Open(@"C:\foo.gif",
System.IO.FileMode.Create)))
{
binWriter.Write(binarydata, 0, (int)len);
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

dr said:
does anyone see what is wrong with my .gif download logic? no matter what
image url i try it downloads a junk file that can't be opened in any paint
program.


System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url);
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream imgStream = myResponse.GetResponseStream();
long len = myResponse.ContentLength;
byte[] binarydata = new byte[len];
System.IO.Stream streambinary = myResponse.GetResponseStream();
streambinary.Read(binarydata, 0, (int)len);
using(System.IO.BinaryWriter binWriter = new
System.IO.BinaryWriter(System.IO.File.Open(@"C:\foo.gif",
System.IO.FileMode.Create)))
{
binWriter.Write(binarydata, 0, (int)len);
}

You read the data from the stream without considering the result. The
Read method returns an integer that is the number of bytes actually
read, which may be smaller than the number of bytes requested.

When you read the data, the buffer will only be partially filled with
data, then you save the entire buffer as if it all contained valid data.
This will produce a broken file.

You have to get the return value from the Read call and use that to
determine how much data you have gotten, and loop until you have got all
the data.
 
T

Thomas Due

Michael said:
Please don't multipost.

One should think, this kind of reply would only be necessary in one group and not both...

--
Thomas Due
Posted with XanaNews version 1.18.1.6

"A military operation involves deception. Even though you are
competent, appear to be incompetent. Though effective, appear
to be ineffective."
-- Sun-tzu, The Art of War. Strategic Assessments
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

dr said:
does anyone see what is wrong with my .gif download logic? no matter what
image url i try it downloads a junk file that can't be opened in any paint
program.

Use this code

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(
"http://www..com/update.zip");
HttpWebResponse response =(HttpWebResponse) request.GetResponse();
Stream source = response.GetResponseStream();
string targetFile = Config.CodeBase + "\\" +
DateTime.Now.Ticks.ToString() + ".zip";
FileStream writer = new FileStream( targetFile, FileMode.Create);
byte[] buffer = new byte[4098];
int readed=0;
while( (readed=source.Read( buffer, 0, 4098))>0)
{
writer.Write( buffer, 0, readed);
}
writer.Close();
source.Close();
 
M

Michael C

Thomas Due said:
One should think, this kind of reply would only be necessary in one group
and not both...

Shouldn't both groups know that maybe the question has been answered
elsewhere?



--
Thomas Due
Posted with XanaNews version 1.18.1.6

"A military operation involves deception. Even though you are
competent, appear to be incompetent. Though effective, appear
to be ineffective."
-- Sun-tzu, The Art of War. Strategic Assessments
 
Top