Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am creating a web app that will allow users to download files from the server and save it to their local machine, Here is my code for downloading the files.
C#
using (WebClient client = new WebClient())
                        {
                            Uri srcPath= new Uri(urlString);
                            
                            client.DownloadFile(srcPath,destPath);

                        }


The srcPath is the path from the server, and the destination path is the path where the files will be downloaded to the client machine. I always get an error that path is not recognized. And when I change the destPath to just "C:\" it downloads the files but not the the local machine, but to the server's drive C: .. I have been doing some research on this and can't find an article or solution that describes the same problem. You help is greatly appreciated.

Thanks,
Posted

Put the below code

//Downloading file form Http Url
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(url, filePath);
//CompleteDownloading file form Http Url
 
Share this answer
 
Comments
Franco Cipriano 18-Apr-13 4:06am    
Hi,

This is more like the same as what I have on my code. Will the url be a string?or a URI object?also does the filePath require a filename?
aamirsajjad 25-Jul-13 5:18am    
Please do this , Also save file on client Machine. i am facing the same problem in asp.net , solution is there

string url = deg + "\\" + file;
WebClient wc = null;

string path1 = "~/temp/";
string filename = DateTime.Now.Ticks + ".tmp";

wc = new WebClient();

wc.DownloadFile(url, Server.MapPath(path1 + filename));

Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("ContentType", "application/octet-stream");
Response.AddHeader("Content-Disposition", "attachment;filename=" + file);

Response.WriteFile(Server.MapPath(path1 + filename));

Response.End();
Kamalkant(kk) 18-Apr-13 4:18am    
Url be the http://~ address
Franco Cipriano 25-Jul-13 9:23am    
Thanks,
what's the variable deg and file?
Franco Cipriano 18-Apr-13 8:17am    
My files are located in a different server..what should be the url format? http://<ipaddress>//folder_path ... is that the format? thanks
Please do this , Also save file on client Machine. i am facing the same problem in asp.net , solution is there string url = deg + "\\" + file; WebClient wc = null; string path1 = "~/temp/"; string filename = DateTime.Now.Ticks + ".tmp"; wc = new WebClient(); wc.DownloadFile(url, Server.MapPath(path1 + filename)); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("ContentType", "application/octet-stream"); Response.AddHeader("Content-Disposition", "attachment;filename=" + file); Response.WriteFile(Server.MapPath(path1 + filename)); Response.End();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900