Download binary file (http)

  • Thread starter Thread starter Fredrik
  • Start date Start date
F

Fredrik

How can I download a binary file (like .doc or .pdf) from http to my
harddrive using the .net framework?

WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
BinaryReader bsRead = new BinaryReader(objResponse.GetResponseStream()
FileStream fs = new FileStream("test.doc", FileMode.CreateNew);
while(bsRead.PeekChar() != -1)
{
cbuf=bsRead.ReadByte();
fs.WriteByte(cbuf);
}

is one of the method I've tried. All of them failed becuase seeing in
the base stream wasn't supported. All tutorals and examples i've seen,
both on the internet and in msdn, deals with reading text, or a binary
file of predefined size.

Downloading a .doc file using Webclient.DownloadFile(...) resulted in
a file that seemed to have been treated like text, not binary.

Please help!
 
Fredrik said:
How can I download a binary file (like .doc or .pdf) from http to my
harddrive using the .net framework?

WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
BinaryReader bsRead = new BinaryReader(objResponse.GetResponseStream()
FileStream fs = new FileStream("test.doc", FileMode.CreateNew);
while(bsRead.PeekChar() != -1)
{
cbuf=bsRead.ReadByte();
fs.WriteByte(cbuf);
}

is one of the method I've tried. All of them failed becuase seeing in
the base stream wasn't supported. All tutorals and examples i've seen,
both on the internet and in msdn, deals with reading text, or a binary
file of predefined size.

Why are you creating the BinaryReader? Just read from the stream and
write to your output stream:

WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
byte[] buffer = new byte[32768];
using (Stream input = objResponse.GetResponseStream())
{
using (FileStream output = new FileStream ("test.doc",
FileMode.CreateNew))
{
int bytesRead;

while ( (bytesRead=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
 
Hi,

The base stream which is returned by using
GetResponseStream doesn't support Seeking. Since it
doesn't support Seeking, PeekChar method always returns -
1. You can use the the Read method of the stream to read
the contents of the stream.

sample code snippet for doing the same is given below,

byte[] buffer = new byte[4096];
int length;

length = respStream.Read(buffer,0,4096);
while ( length > 0)
{
fs.Write(buffer,0,length);
length = respStream.Read(buffer,0,4096);
}
fs.Close();

Hope this helps...

Regards,
Madhu

MVP | MCSD.NET
 
Hi,
Could you give me some advices? I just begin the work. I don't know how to
use RCW. I want to make a custom interface. I know the automation Interface
need proxy-stub dll and automation wrapper, and then code the application
program. how about custom interface? only proxy-stub dll is ok? and then
build the custom interface. as for the proxy-stub dll, is there any
difference between different company's OPC sever. or can I use a sample
proxy-stub dll of OPC foundation to build a special custom interface?

Thank you
Best regards
seaweed
 
WebClient client = new WebClient();
client.DownloadFile(url, saveasfilename);
client.Dispose();
 
Fredrik,

It should be pointed out that you are getting the WebRequest
incorrectly. You should be using the following line:

WebRequest objRequest = WebRequest.Create(url);

The static Create method is used to create instances of the
HttpWebRequest. What you are doing isn't wrong (as it works), but it isn't
the recommended way of doing it.
 
Back
Top