如何从C#网站下载文件

是否可以从Windows应用程序窗体中的网站下载文件并将其放入特定目录?

使用WebClient类 :

using System.Net; //... WebClient Client = new WebClient (); Client.DownloadFile("http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png", @"C:\folder\stackoverflowlogo.png"); 

使用WebClient.DownloadFile

 using (WebClient client = new WebClient()) { client.DownloadFile("http://csharpindepth.com/Reviews.aspx", @"c:\Users\Jon\Test\foo.txt"); } 

当然,你只是使用HttpWebRequest

设置HttpWebRequest后,可以将响应stream保存到文件StreamWriterBinaryWriterTextWriter取决于mimetype),并在硬盘上有一个文件。

编辑:忘记WebClient 。 这很好,除非你只需要使用GET来检索你的文件。 如果网站要求你发布信息,你将不得不使用HttpWebRequest ,所以我要离开我的答案了。

在提出请求之前,您可能需要知道文件下载期间的状态或使用凭据。

这是一个涵盖这些选项的例子:

 Uri ur = new Uri("http://remotehost.dohttp://img.dovov.comimg.jpg"); using (WebClient client = new WebClient()) { //client.Credentials = new NetworkCredential("username", "password"); String credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("Username" + ":" + "MyNewPassword")); client.Headers[HttpRequestHeader.Authorization] = $"Basic {credentials}"; client.DownloadProgressChanged += WebClientDownloadProgressChanged; client.DownloadDataCompleted += WebClientDownloadCompleted; client.DownloadFileAsync(ur, @"C:\path\newImage.jpg"); } 

而callback的function实现如下:

 void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { Console.WriteLine("Download status: {0}%.", e.ProgressPercentage); // updating the UI Dispatcher.Invoke(() => { progressBar.Value = e.ProgressPercentage; }); } void WebClientDownloadCompleted(object sender, DownloadDataCompletedEventArgs e) { Console.WriteLine("Download finished!"); } 

Lambda符号:处理事件的其他可能选项

 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(delegate(object sender, DownloadProgressChangedEventArgs e) { Console.WriteLine("Download status: {0}%.", e.ProgressPercentage); // updating the UI Dispatcher.Invoke(() => { progressBar.Value = e.ProgressPercentage; }); }); client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(delegate(object sender, DownloadDataCompletedEventArgs e){ Console.WriteLine("Download finished!"); }); 

我们可以做得更好

 client.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) => { Console.WriteLine("Download status: {0}%.", e.ProgressPercentage); // updating the UI Dispatcher.Invoke(() => { progressBar.Value = e.ProgressPercentage; }); }; client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) => { Console.WriteLine("Download finished!"); }; 

要么

 client.DownloadProgressChanged += (o, e) => { Console.WriteLine($"Download status: {e.ProgressPercentage}%."); // updating the UI Dispatcher.Invoke(() => { progressBar.Value = e.ProgressPercentage; }); }; client.DownloadDataCompleted += (o, e) => { Console.WriteLine("Download finished!"); }; 

您可以使用此代码将文件从网站下载到桌面:

 using System.Net; WebClient Client = new WebClient (); client.DownloadFileAsync(new Uri("http://www.Address.com/File.zip"), Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "File.zip"); 

试试这个例子:

 public void TheDownload(string path) { System.IO.FileInfo toDownload = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(path)); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + toDownload.Name); HttpContext.Current.Response.AddHeader("Content-Length", toDownload.Length.ToString()); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.WriteFile(patch); HttpContext.Current.Response.End(); } 

实现过程如下:

 TheDownload("@"c:\Temporal\Test.txt""); 

来源: http : //www.systemdeveloper.info/2014/03/force-downloading-file-from-c.html

您也可以在WebClient类中使用DownloadFileAsync方法。 它使用指定的URI将资源下载到本地文件。 此方法也不会阻塞调用线程。

样品:

  webClient.DownloadFileAsync(new Uri("http://www.example.com/file/test.jpg"), "test.jpg"); 

了解更多信息:

http://csharpexamples.com/download-files-synchronous-asynchronous-url-c/