473,396 Members | 1,777 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,396 software developers and data experts.

Downloading a file from a webserver to local hard drive.

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());
Nov 16 '05 #1
2 2671
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]
- mv*@spam.guard.caspershouse.com

"Trygve Lorentzen" <trygveloAThaldenDOTnet> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
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());

Nov 16 '05 #2
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Trygve Lorentzen" <trygveloAThaldenDOTnet> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
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());


Nov 16 '05 #3

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

Similar topics

3
by: wj | last post by:
Hi all, I've got some problems uploading files using apache and php on a local Windows PC. I'm using the file upload example from the manual, but i can't get it to work. I don't get an error...
8
by: Lam | last post by:
HI anyone knows how can I open a mapped network file in C#? I try string file = @"T:\file.txt"; it shows me the error: "Could not find a part of the path" but if I copy the file to my C dirve,...
2
by: Michal Valent | last post by:
Hello, if I try to read a file on the mapped network drive, this error apears: Logon failure: unknown user name or bad password. How to set up permissions for IUSR_MACHINENAME on the mapped...
2
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am...
2
by: robert d via AccessMonster.com | last post by:
I need to determine for sure if a particular fie is on the user's local PC or on the server. I have the complete path of the file, let's call it FullFilePath How can I unequivocally determine...
23
by: wylbur37 | last post by:
I'm running an Apache server on my own computer (Windows XP Pro). I wrote a simple PHP script (called test3.php) that I'm running by putting the following URL in the address bar of the browser...
2
by: Gerhard | last post by:
I have a .net application that I want to run in a DMZ, with the SQL Server and file system behind another firewall. Is there a secure way to get to files from my application, or would it be better...
1
by: Vic Spainhower | last post by:
Hello, I am trying to download a file to my local machine using PHP. However, when the script runs the file is placed in the folder assigned to the ftp user on the Web server instead of...
7
by: vstud | last post by:
Hello, I need some begginers help here with the following: I developed ASP 2.0 application on my personal machine which is using the wwwroot localhost and everything works fine. my question...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.