能否在Windows应用程序中从网站下载文件并将其放入特定目录中?
能否在Windows应用程序中从网站下载文件并将其放入特定目录中?
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");
}
WebClient
答案以展示异步版本。 - Jon Skeet在文件下载期间或在发出请求之前,您可能需要了解状态或使用凭据。
这里有一个涵盖这些选项的示例:
Uri ur = new Uri("http://remotehost.do/images/img.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");
}
回调函数的实现如下:
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!");
}
(Ver 2) - 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!");
});
(版本 3) - 我们可以做得更好
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!");
};
(版本4) - 或
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!");
};
async Task
而不是async void
,请查看这个答案,它使用await webClient.DownloadFileTaskAsync(...)
,因此不需要DownloadDataCompleted
事件。 - MikeT没问题,您可以使用 HttpWebRequest
。
一旦您设置好了 HttpWebRequest
,您就可以将响应流保存为一个文件 StreamWriter
(使用 BinaryWriter
或TextWriter
取决于mimetype) 并将其保存到硬盘上。
编辑:忘记了 WebClient
。如果网站要求您提交信息,那么除了只需要使用GET
检索文件之外,它也可以很好地工作。因此,我保留我的答案。
using System.Net;
WebClient client = new WebClient ();
client.DownloadFileAsync(new Uri("http://www.Address.com/File.zip"), Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "File.zip");
你也可以使用 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/
尝试这个例子:
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