473,785 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Crea te(downloadLoc) ;
long length = req.ContentLeng th;

WebResponse rsp = req.GetResponse ();

Stream inStream = rsp.GetResponse Stream();

File.Delete(App lication.Startu pPath + "Test.tmp") ;

FileStream fs = File.Create(App lication.Startu pPath + "Transport.tmp" );
while(inStream. Position < inStream.Length )

fs.WriteByte((b yte)inStream.Re adByte());
Nov 16 '05 #1
2 2693
Trygve,

Will this work?

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

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

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

// Download the file.
wc.DownloadFile ("http://www.foo.com/files/Test.exe", Application.Sta rtupPath
+ "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.co m

"Trygve Lorentzen" <trygveloAThald enDOTnet> wrote in message
news:el******** ******@TK2MSFTN GP10.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.Crea te(downloadLoc) ;
long length = req.ContentLeng th;

WebResponse rsp = req.GetResponse ();

Stream inStream = rsp.GetResponse Stream();

File.Delete(App lication.Startu pPath + "Test.tmp") ;

FileStream fs = File.Create(App lication.Startu pPath + "Transport.tmp" );
while(inStream. Position < inStream.Length )

fs.WriteByte((b yte)inStream.Re adByte());

Nov 16 '05 #2
That worked nice, except I had to add this:

Path.DirectoryS eparatorChar to the line below
Application.Sta rtupPath + Path.DirectoryS eparatorChar + "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.c om> wrote in
message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Trygve,

Will this work?

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

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

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

// Download the file.
wc.DownloadFile ("http://www.foo.com/files/Test.exe", Application.Sta rtupPath + "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.co m

"Trygve Lorentzen" <trygveloAThald enDOTnet> wrote in message
news:el******** ******@TK2MSFTN GP10.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.Crea te(downloadLoc) ;
long length = req.ContentLeng th;

WebResponse rsp = req.GetResponse ();

Stream inStream = rsp.GetResponse Stream();

File.Delete(App lication.Startu pPath + "Test.tmp") ;

FileStream fs = File.Create(App lication.Startu pPath + "Transport.tmp" );
while(inStream. Position < inStream.Length )

fs.WriteByte((b yte)inStream.Re adByte());


Nov 16 '05 #3

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

Similar topics

3
660
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 message, it's asif nothing happens. I can't find the file that should be uploaded on my harddisk. Has anyone a working example for file-uploads on a local server with the settings for the php.ini file? Any tips?
8
11853
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, and use @"C:\file.txt"; it worked Thanks a lot
2
3933
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 network drive? Thaks
2
2438
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 impersonating the domain user account and downloading the file, It seems the impersonate is got success, but its not download the file. Its simply downloading some junk pdf file instead of original file.
2
3478
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 if this is on the local PC or on a server or other machine. The local computer may have more than one hard drive but if the file is on any of these local hard drives I would want the code to produce a result that the file is on the local PC. I...
23
5384
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 (Firefox) .... http://localhost/test3.php The script generates the following link using the echo statement ...
2
2382
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 to have a reverse proxy server in the DMZ and the webserver behind the second firewall?
1
2395
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 downloading to my local C drive. After the following script runs I find a file named C:\TestShowID.txt in the user's FTP folder on the web server. Can some tell me why this happens? The local machine is a Windows XP machine. Thanks,
7
1486
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 is how do I move/transform my asp application to work on our webserver (how do i deploy it ). ? to be more specific, in my asp application (using the ms visual studio 2005 ide) the query points to my local Access d.b. that reside on local pc hard...
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.