Downloading a file from a webserver to local hard drive.

  • Thread starter Trygve Lorentzen
  • Start date
T

Trygve Lorentzen

Hi,

I want to read a file from a http request (say, http://www.foo.com/test.exe)
to my local hard drive. How can I do that? I need specific code examples
here, not just "use HttpRequest and get a Stream and write that stream to
disc with BinaryWriter". This is what I have gotten to so far, it probably
looks stupid if you know how to do it, but I can't find much help on the web

Uri downloadLoc = new Uri("http://www.foo.com/files/Test.exe");

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(downloadLoc);


long length = req.ContentLength;

WebResponse rsp = req.GetResponse();

Stream inStream = rsp.GetResponseStream();

File.Delete(Application.StartupPath + "Test.tmp");

FileStream fs = File.Create(Application.StartupPath + "Transport.tmp");


while(inStream.Position < inStream.Length)

fs.WriteByte((byte)inStream.ReadByte());
 
N

Nicholas Paldino [.NET/C# MVP]

Trygve,

Will this work?

// Create a web client.
WebClient wc = new WebClient();

// The file name.
string fileName = Application.StartupPath + "Test.tmp";

// Delete the old file if it exists.
File.Delete(fileName);

// Download the file.
wc.DownloadFile("http://www.foo.com/files/Test.exe", Application.StartupPath
+ "Test.tmp");

Of course, what you had isn't that far off, you should just read to the
end of the stream, and not worry about the content length (servers don't
always serve that up).

Hope this helps.
 
T

Trygve Lorentzen

That worked nice, except I had to add this:

Path.DirectorySeparatorChar to the line below
Application.StartupPath + Path.DirectorySeparatorChar + "Test.tmp";



Well, that's all good, but now I figured I should provide a statusbar to
allow the user to see how far the download has come. Any ideas? Maybe I have
to use a Stream after all and a counter? It might be difficult since I don't
know the length of the file anyway (the server doesn't seem to provide that
information, IIS 5.0, Win2000 Server).

Thanks for your help so far, I really appreciate it!

Cheers,

Trygve



Nicholas Paldino said:
Trygve,

Will this work?

// Create a web client.
WebClient wc = new WebClient();

// The file name.
string fileName = Application.StartupPath + "Test.tmp";

// Delete the old file if it exists.
File.Delete(fileName);

// Download the file.
wc.DownloadFile("http://www.foo.com/files/Test.exe", Application.StartupPath
+ "Test.tmp");

Of course, what you had isn't that far off, you should just read to the
end of the stream, and not worry about the content length (servers don't
always serve that up).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Trygve Lorentzen said:
Hi,

I want to read a file from a http request (say,
http://www.foo.com/test.exe)
to my local hard drive. How can I do that? I need specific code examples
here, not just "use HttpRequest and get a Stream and write that stream to
disc with BinaryWriter". This is what I have gotten to so far, it probably
looks stupid if you know how to do it, but I can't find much help on the
web

Uri downloadLoc = new Uri("http://www.foo.com/files/Test.exe");

HttpWebRequest req = (HttpWebRequest) WebRequest.Create(downloadLoc);


long length = req.ContentLength;

WebResponse rsp = req.GetResponse();

Stream inStream = rsp.GetResponseStream();

File.Delete(Application.StartupPath + "Test.tmp");

FileStream fs = File.Create(Application.StartupPath + "Transport.tmp");


while(inStream.Position < inStream.Length)

fs.WriteByte((byte)inStream.ReadByte());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top