C#使用WebClient實現(xiàn)上傳下載
一、概述
System.Net.WebClient屬于高層類、使用簡單。均支持異步版本。支持http,https,fpt,files等URI。
建議不要將
WebClient類用于新的開發(fā)。Net4.5及以上請改用 System.Net.Http.HttpClient 類。
二、下載
1、OpenRead:打開一個可讀的Stream。
對于FTP資源,默認使用RETR命令;對于HTTP資源,默認使用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)址的真實文件名
獲取http頭部信息的內(nèi)容。
Content-disposition 是 MIME 協(xié)議的擴展,MIME 協(xié)議指示 MIME 用戶代理如何顯示附加的文件。
Content-disposition其實可以控制用戶請求所得的內(nèi)容存為一個文件的時候提供一個默認的文件名,文件直接在瀏覽器上顯示或者在訪問時彈出文件下載對話框。
形如:”Content-Disposition: attachment;filename=FileName.txt“。
當你在響應類型為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資源,默認使用STOR命令;對于HTTP資源,默認使用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.dhdzp.com:2105/article/247999.htm?order=true”);
- OriginalString 獲取傳遞給 Uri 構(gòu)造函數(shù)的原始 URI 字符串。//www.dhdzp.com:2105/article/247999.htm?order=true
- Scheme 獲取此 URI 的方案名稱。http
- IsFile 獲取一個值,該值指示指定的 Uri 是否為文件 URI。false
- Host 獲取此實例的主機部分。www.dhdzp.com
- HostNameType 獲取 URI 中指定的主機名的類型。DNS
- Port 獲取此 URI 的端口號。2105
- IsDefaultPort 獲取一個值,該值指示 URI 的端口值是否為此方案的默認值。false
- AbsolutePath 獲取 URI 的絕對路徑。/article/247999.htm
- Query 獲取指定 URI 中包括的任何查詢信息。?order=true
- PathAndQuery 獲取用問號 (?) 分隔的 AbsolutePath 和 Query 屬性。/article/247999.htm?order=true
到此這篇關(guān)于C#使用WebClient實現(xiàn)上傳下載的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# List 并發(fā)丟數(shù)據(jù)問題原因及解決方案
這篇文章主要介紹了C# List 并發(fā)丟數(shù)據(jù)問題原因及解決方案,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02
C#結(jié)合JavaScript實現(xiàn)秒殺倒計時的方法
這篇文章主要介紹了C#結(jié)合JavaScript實現(xiàn)秒殺倒計時的方法,涉及C#結(jié)合javascript操作時間的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
C#使用Oracle.ManagedDataAccess.dll組件連接Oracle數(shù)據(jù)庫
這篇文章介紹了C#使用Oracle.ManagedDataAccess.dll組件連接Oracle數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C#實現(xiàn)appSettings節(jié)點讀取與修改的方法
這篇文章主要介紹了C#實現(xiàn)appSettings節(jié)點讀取與修改的方法,是非常實用的技巧,需要的朋友可以參考下2014-10-10

