C#使用WebClient實(shí)現(xiàn)上傳下載
一、概述
System.Net.WebClient屬于高層類、使用簡單。均支持異步版本。支持http,https,fpt,files等URI。
建議不要將
WebClient
類用于新的開發(fā)。Net4.5及以上請改用 System.Net.Http.HttpClient 類。
二、下載
1、OpenRead:打開一個可讀的Stream。
對于FTP資源,默認(rèn)使用RETR命令;對于HTTP資源,默認(rèn)使用Get方法。
Stream= client.OpenRead(serverUri):
舉例
WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); client.Encoding = Encoding.GetEncoding("gb2312"); client.Credentials = new NetworkCredential("csp", "welcome"); Stream data = client.OpenRead(url);//OpenRead為下載的數(shù)據(jù)打開一個只讀的流 StreamReader reader = new StreamReader(data, Encoding.GetEncoding("gb2312")); sting s = reader.ReadToEnd(); reader.Close(); return s;
2、DownloadData:以byte[]形式下載資源。
byte[] data= client.DownloadData(serverUri):
3、DownloadFile:將資源下載在本地文件。
void client.DownloadFile(serverUri,localFile):
4、DownloadString:以string的形式下載資源。
string content =client.DownloadString(serverUri):
5、事件
- DownloadProgressChanged事件:
- Download***Completed事件
6、獲取下載網(wǎng)址的真實(shí)文件名
獲取http頭部信息的內(nèi)容。
Content-disposition 是 MIME 協(xié)議的擴(kuò)展,MIME 協(xié)議指示 MIME 用戶代理如何顯示附加的文件。
Content-disposition其實(shí)可以控制用戶請求所得的內(nèi)容存為一個文件的時候提供一個默認(rèn)的文件名,文件直接在瀏覽器上顯示或者在訪問時彈出文件下載對話框。
形如:”Content-Disposition: attachment;filename=FileName.txt“。
當(dāng)你在響應(yīng)類型為application/octet- stream情況下使用了這個頭信息的話,那就意味著你不想直接顯示內(nèi)容,而是彈出一個"文件下載"的對話框
WebClient client = new WebClient(); byte[] data = client.DownloadData(fileUrl); var mc = Regex.Matches(Server.UrlDecode(client.ResponseHeaders["Content-Disposition"]), @"filename=(.+)"); string filename = mc[0].Groups[1].Value;
三、上傳
1、OpenWrite:打開一個可寫的流。
對于FTP資源,默認(rèn)使用STOR命令;對于HTTP資源,默認(rèn)使用POST方法。
Strean =client.OpenWrite(serverUri);
2、UploadData:將byte[]數(shù)據(jù)上傳到serverUri。
byte[] =client.UploadData(serverUri,byte[]);
3、UploadFile:將本地文件上傳到serverUri。
byte[] =client.UploadFile(serverUri,localFile);
舉例:
WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; client.Credentials = new NetworkCredential("csp", "welcome"); client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback); client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCompletedCallback); client.UploadFileAsync(new Uri(uriString + Path.GetFileName(localfileName)), null, localfileName, progressbarfrom); /// /// 上傳過程處理 /// /// /// private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e) { (e.UserState as ProgressBar).Value = e.ProgressPercentage; } private static void UploadFileCompletedCallback(object sender, UploadFileCompletedEventArgs e) { if (e.Error == null) MessageBox.Show("完成上傳"); else throw e.Error; }
4、UploadValues:將指定的名/值集合到serverUri。
byte[] =client.UploadValues(serverUri,namevalueCollection);
5、事件:
- UploadProgressChanged:e.ProcessPercentage,e.TatalBytesToReceive,e.BytesReceived;
- Upload***Completed: e.Cancelled,e.Error,E.UserState;
四、System.Url類(統(tǒng)一資源表示符)
Uri url=new Uri(”//www.dbjr.com.cn:2105/article/247999.htm?order=true”);
- OriginalString 獲取傳遞給 Uri 構(gòu)造函數(shù)的原始 URI 字符串。//www.dbjr.com.cn:2105/article/247999.htm?order=true
- Scheme 獲取此 URI 的方案名稱。http
- IsFile 獲取一個值,該值指示指定的 Uri 是否為文件 URI。false
- Host 獲取此實(shí)例的主機(jī)部分。www.dbjr.com.cn
- HostNameType 獲取 URI 中指定的主機(jī)名的類型。DNS
- Port 獲取此 URI 的端口號。2105
- IsDefaultPort 獲取一個值,該值指示 URI 的端口值是否為此方案的默認(rèn)值。false
- AbsolutePath 獲取 URI 的絕對路徑。/article/247999.htm
- Query 獲取指定 URI 中包括的任何查詢信息。?order=true
- PathAndQuery 獲取用問號 (?) 分隔的 AbsolutePath 和 Query 屬性。/article/247999.htm?order=true
到此這篇關(guān)于C#使用WebClient實(shí)現(xiàn)上傳下載的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#?Winform實(shí)現(xiàn)進(jìn)度條顯示
這篇文章主要為大家詳細(xì)介紹了C#?Winform實(shí)現(xiàn)進(jìn)度條顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08C#中的不可變數(shù)據(jù)類型介紹(不可變對象、不可變集合)
這篇文章主要介紹了C#中的不可變數(shù)據(jù)類型介紹(不可變對象、不可變集合),本文講解了不可變對象、自定義不可變集合、Net提供的不可變集合、不可變優(yōu)點(diǎn)、不可變對象缺點(diǎn)等內(nèi)容,需要的朋友可以參考下2015-04-04C#如何實(shí)現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù)
這篇文章主要介紹了C#如何實(shí)現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法
下面小編就為大家分享一篇C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01