C#使用WebClient實(shí)現(xiàn)上傳下載
一、概述
System.Net.WebClient屬于高層類、使用簡(jiǎn)單。均支持異步版本。支持http,https,fpt,files等URI。
建議不要將
WebClient
類用于新的開發(fā)。Net4.5及以上請(qǐng)改用 System.Net.Http.HttpClient 類。
二、下載
1、OpenRead:打開一個(gè)可讀的Stream。
對(duì)于FTP資源,默認(rèn)使用RETR命令;對(duì)于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ù)打開一個(gè)只讀的流 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í)可以控制用戶請(qǐng)求所得的內(nèi)容存為一個(gè)文件的時(shí)候提供一個(gè)默認(rèn)的文件名,文件直接在瀏覽器上顯示或者在訪問(wèn)時(shí)彈出文件下載對(duì)話框。
形如:”Content-Disposition: attachment;filename=FileName.txt“。
當(dāng)你在響應(yīng)類型為application/octet- stream情況下使用了這個(gè)頭信息的話,那就意味著你不想直接顯示內(nèi)容,而是彈出一個(gè)"文件下載"的對(duì)話框
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:打開一個(gè)可寫的流。
對(duì)于FTP資源,默認(rèn)使用STOR命令;對(duì)于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); /// /// 上傳過(guò)程處理 /// /// /// 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(”//chabaoo.cn:2105/article/247999.htm?order=true”);
- OriginalString 獲取傳遞給 Uri 構(gòu)造函數(shù)的原始 URI 字符串。//chabaoo.cn:2105/article/247999.htm?order=true
- Scheme 獲取此 URI 的方案名稱。http
- IsFile 獲取一個(gè)值,該值指示指定的 Uri 是否為文件 URI。false
- Host 獲取此實(shí)例的主機(jī)部分。chabaoo.cn
- HostNameType 獲取 URI 中指定的主機(jī)名的類型。DNS
- Port 獲取此 URI 的端口號(hào)。2105
- IsDefaultPort 獲取一個(gè)值,該值指示 URI 的端口值是否為此方案的默認(rèn)值。false
- AbsolutePath 獲取 URI 的絕對(duì)路徑。/article/247999.htm
- Query 獲取指定 URI 中包括的任何查詢信息。?order=true
- PathAndQuery 獲取用問(wèn)號(hào) (?) 分隔的 AbsolutePath 和 Query 屬性。/article/247999.htm?order=true
到此這篇關(guān)于C#使用WebClient實(shí)現(xiàn)上傳下載的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#?Winform實(shí)現(xiàn)進(jìn)度條顯示
這篇文章主要為大家詳細(xì)介紹了C#?Winform實(shí)現(xiàn)進(jìn)度條顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C# 6.0的屬性(Property)的語(yǔ)法與初始值詳解
下面小編就為大家?guī)?lái)一篇C# 6.0的屬性(Property)的語(yǔ)法與初始值詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl單元格編輯驗(yàn)證的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08C#中的不可變數(shù)據(jù)類型介紹(不可變對(duì)象、不可變集合)
這篇文章主要介紹了C#中的不可變數(shù)據(jù)類型介紹(不可變對(duì)象、不可變集合),本文講解了不可變對(duì)象、自定義不可變集合、Net提供的不可變集合、不可變優(yōu)點(diǎn)、不可變對(duì)象缺點(diǎn)等內(nèi)容,需要的朋友可以參考下2015-04-04C#如何實(shí)現(xiàn)dataGridView動(dòng)態(tài)綁定數(shù)據(jù)
這篇文章主要介紹了C#如何實(shí)現(xiàn)dataGridView動(dòng)態(tài)綁定數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法
下面小編就為大家分享一篇C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01