PC Review


Reply
Thread Tools Rate Thread

Downloading a file from a webserver to local hard drive.

 
 
Trygve Lorentzen
Guest
Posts: n/a
 
      5th Jan 2005
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());


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Jan 2005
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 Removed)

"Trygve Lorentzen" <trygveloAThaldenDOTnet> wrote in message
news:(E-Mail Removed)...
> 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());
>
>



 
Reply With Quote
 
Trygve Lorentzen
Guest
Posts: n/a
 
      5th Jan 2005
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 [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:%23$(E-Mail Removed)...
> 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 Removed)
>
> "Trygve Lorentzen" <trygveloAThaldenDOTnet> wrote in message
> news:(E-Mail Removed)...
> > 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());
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
downloading video file on hard drive to DVD =?Utf-8?B?U3Vl?= Windows XP Video 3 25th Apr 2007 12:35 AM
Move web from webserver to my hard drive =?Utf-8?B?QmV2ZXJseS1UZXhhcw==?= Microsoft Frontpage 2 22nd Mar 2005 09:13 AM
Re: downloading file from secure webserver Joerg Jooss Microsoft Dot NET 0 7th Aug 2004 09:44 AM
fp 2003 : can not publish from webserver to hard drive kris Microsoft Frontpage 2 18th Jun 2004 10:30 AM
Copy file from drive to webserver Anders Microsoft Excel Programming 0 18th Sep 2003 08:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:17 AM.