是否可以從Windows應用程序窗體中的網站下載文件並將其放入特定目錄?如何從C#網站下載文件?
56
A
回答
103
using System.Net;
//...
WebClient Client = new WebClient();
Client.DownloadFile("http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png", @"C:\folder\stackoverflowlogo.png");
72
using (WebClient client = new WebClient())
{
client.DownloadFile("http://csharpindepth.com/Reviews.aspx",
@"c:\Users\Jon\Test\foo.txt");
}
12
當然,你只需要使用一個HttpWebRequest
。
一旦你的HttpWebRequest
設置,您可以響應流(根據MIME類型無論是BinaryWriter
,或者TextWriter
)保存到一個文件StreamWriter
,你有你的硬盤驅動器上的文件。
編輯:忘了WebClient
。除非您只需要使用GET
來檢索您的文件,否則這種方式效果不錯。如果該網站要求您提供POST
信息,則必須使用HttpWebRequest
,所以我要離開我的答案。
0
試試這個例子:
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
0
也可以在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/
2
您可以使用此代碼從一個網站下載文件到桌面:
12
在發出請求之前,您可能需要知道文件下載期間的狀態或使用憑據。
下面是覆蓋這些選項的例子:
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!");
}
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!");
};
相關問題
- 1. C#從網站下載.swf文件
- 2. SILVERLIGHT。如何從網站下載文件
- 3. 如何從網站下載.cs文件?
- 4. 如何從網站下載文件?
- 5. 從網站下載文件
- 6. 從網站下載文件
- 7. 如何以編程方式從網站下載文件c#
- 8. 我如何從網站下載文章?
- 9. 如何自動將文件從網站下載到文件夾
- 10. 從網站下載文件解析
- 11. 從網站下載音頻文件
- 12. 從網站下載CSV文件
- 13. 從網站下載文件PHP
- 14. 從網站下載臨時文件
- 15. 從多語言網站下載文件
- 16. 從網站自動下載XLS文件
- 17. 使用Python從網站下載文件
- 18. 從網站下載某個文件
- 19. 從Arduino網站下載強制文件
- 20. 圖書館從網站下載文件?
- 21. 從ASP.NET MVC 2網站下載文件
- 22. 使用python從網站下載文件
- 23. 從webservice下載文件 - 在ASP.NET網站
- 24. 從多個網站下載文件。
- 25. 從各種網站下載apk文件
- 26. 從網站下載txt文件android
- 27. 從網站下載所有PDF文件
- 28. 如何使用Python從網站下載文本文件?
- 29. 如何將其他網站文件下載到我的網站?
- 30. 從sftp站點下載文件 - 如何?
什麼交通工具? FTP? HTTP? – 2009-02-08 07:54:38
米奇的評論是最直接,最準確的答案,哈哈! – Cerebrus 2009-02-08 07:56:50
除非你是.net的新手,否則我會建議搜索MSDN文檔將有所幫助。尋找你想要達到的目標,在這個命名空間中可以適用,看看是否有一個類可以做到這一點:) – shahkalpesh 2009-02-08 08:08:10