是否可以从 Windows 应用程序表单中的网站下载文件并将其放入某个目录?
7 回答
使用WebClient 类:
using System.Net;
//...
WebClient Client = new WebClient ();
Client.DownloadFile("http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png", @"C:\folder\stackoverflowlogo.png");
using (WebClient client = new WebClient())
{
client.DownloadFile("http://csharpindepth.com/Reviews.aspx",
@"c:\Users\Jon\Test\foo.txt");
}
在发出请求之前,您可能需要知道文件下载期间的状态或使用凭据。
这是一个涵盖这些选项的示例:
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!");
});
(Ver 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!");
};
当然,您只需使用HttpWebRequest
.
HttpWebRequest
设置完成后,您可以将响应流保存到一个文件(StreamWriter
取决于mimetypeBinaryWriter
或 a ),并且您的硬盘驱动器上有一个文件。TextWriter
编辑:忘记了WebClient
。除非您只需要使用GET
来检索文件,否则效果很好。如果该站点要求您向其POST
提供信息,则您必须使用 a HttpWebRequest
,因此我将保留我的答案。
您可以使用此代码将文件从网站下载到桌面:
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
您也可以在课堂上使用DownloadFileAsync
方法。WebClient
它将具有指定的资源下载到本地文件URI
。此方法也不会阻塞调用线程。
样本:
webClient.DownloadFileAsync(new Uri("http://www.example.com/file/test.jpg"), "test.jpg");
了解更多信息:
http://csharpexamples.com/download-files-synchronous-asynchronous-url-c/