windows 如何用C#从网站下载文件[已关闭]

ifmq2ha2  于 2023-11-21  发布在  Windows
关注(0)|答案(7)|浏览(240)

已关闭。此问题需要更多focused。目前不接受回答。
**要改进此问题吗?**更新此问题,使其仅针对editing this post的一个问题。

7年前关闭。
Improve this question
是否可以从Windows应用程序形式的网站下载文件并将其放入某个目录?

zpjtge22

zpjtge221#

关于WebClient class

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

字符串

oymdgrw7

oymdgrw72#

使用WebClient.DownloadFile

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

字符串

cigdeys3

cigdeys33#

您可能需要在文件下载期间了解状态,或在发出请求之前使用凭据。

以下是一个包含这些选项的示例:

  1. Uri ur = new Uri("http://remotehost.do/images/img.jpg");
  2. using (WebClient client = new WebClient()) {
  3. //client.Credentials = new NetworkCredential("username", "password");
  4. String credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("Username" + ":" + "MyNewPassword"));
  5. client.Headers[HttpRequestHeader.Authorization] = $"Basic {credentials}";
  6. client.DownloadProgressChanged += WebClientDownloadProgressChanged;
  7. client.DownloadDataCompleted += WebClientDownloadCompleted;
  8. client.DownloadFileAsync(ur, @"C:\path\newImage.jpg");
  9. }

字符串
回调函数实现如下:

  1. void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  2. {
  3. Console.WriteLine("Download status: {0}%.", e.ProgressPercentage);
  4. // updating the UI
  5. Dispatcher.Invoke(() => {
  6. progressBar.Value = e.ProgressPercentage;
  7. });
  8. }
  9. void WebClientDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
  10. {
  11. Console.WriteLine("Download finished!");
  12. }

(版本2)- Lambda表示法:处理事件的其他可能选项

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

(Ver 3)-我们可以做得更好

  1. client.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) =>
  2. {
  3. Console.WriteLine("Download status: {0}%.", e.ProgressPercentage);
  4. // updating the UI
  5. Dispatcher.Invoke(() => {
  6. progressBar.Value = e.ProgressPercentage;
  7. });
  8. };
  9. client.DownloadDataCompleted += (object sender, DownloadDataCompletedEventArgs e) =>
  10. {
  11. Console.WriteLine("Download finished!");
  12. };

(版本4)-或者

  1. client.DownloadProgressChanged += (o, e) =>
  2. {
  3. Console.WriteLine($"Download status: {e.ProgressPercentage}%.");
  4. // updating the UI
  5. Dispatcher.Invoke(() => {
  6. progressBar.Value = e.ProgressPercentage;
  7. });
  8. };
  9. client.DownloadDataCompleted += (o, e) =>
  10. {
  11. Console.WriteLine("Download finished!");
  12. };

展开查看全部
cetgtptt

cetgtptt4#

当然,你只需要使用HttpWebRequest
设置好HttpWebRequest后,您可以将响应流保存到文件StreamWriter(根据mimetype,可以是BinaryWriter,也可以是TextWriter)中,并且您的硬盘上有一个文件。
编辑:忘记了WebClient。这很好用,除非你只需要使用GET来检索你的文件。如果网站要求你向它提供POST信息,你就必须使用HttpWebRequest,所以我留下我的答案。

wnavrhmk

wnavrhmk5#

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

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

字符串

ni65a41a

ni65a41a6#

你也可以在WebClient类中使用DownloadFileAsync方法。它将指定的URI资源下载到本地文件。而且这个方法不会阻塞调用线程。
样品名称:

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

字符串

更多信息:

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

rsl1atfo

rsl1atfo7#

试试这个例子:

  1. public void TheDownload(string path)
  2. {
  3. System.IO.FileInfo toDownload = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(path));
  4. HttpContext.Current.Response.Clear();
  5. HttpContext.Current.Response.AddHeader("Content-Disposition",
  6. "attachment; filename=" + toDownload.Name);
  7. HttpContext.Current.Response.AddHeader("Content-Length",
  8. toDownload.Length.ToString());
  9. HttpContext.Current.Response.ContentType = "application/octet-stream";
  10. HttpContext.Current.Response.WriteFile(path);
  11. HttpContext.Current.Response.End();
  12. }

字符串
具体实现如下:

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


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

展开查看全部

相关问题