System.Net.WebClient //提供從 URI 識別的資源處傳送和接收資料的通用方法。
.Credentials //取得或設定傳送至主機並用於驗證要求的網路認證。
DownloadData(String) //下載具有指定之 URI 的資源做為 Byte 陣列。
System.Net.NetworkCredential //提供密碼架構的驗證 (Authentication) 機制 (例如基本、摘要、NTLM 和 Kerberos 驗證) 的認證。
例子:
System.Net.WebClient oWebClient = new System.Net.WebClient();
oWebClient.Credentials = new System.Net.NetworkCredential("ws_access", "access.ws+itec");
byte[] fileStream = oWebClient.DownloadData(sUrl);
string tempfile = Guid.NewGuid() + ".pdf";
string fileLocalPath = System.IO.Path.Combine( Environment.GetEnvironmentVariable("temp"),tempfile);
FileStream fs = new FileStream(fileLocalPath, FileMode.Create);
fs.Write(fileStream, 0, fileStream.Length);
fs.Close();
Response.Clear();
Response.ContentType = string.Empty;
Response.BufferOutput = false;
Response.AppendHeader("content-disposition", "attachment;filename=" + Path.GetFileName(fileLocalPath));
Response.WriteFile(fileLocalPath);
Response.Flush();
System.IO.File.Delete(fileLocalPath);
Response.Close();
Content-Disposition 的使用和注意事項
作者︰Qihangnet 來源︰Qihangnet的博客 時間︰2010-1-12
最近不少 Web 技術圈內的朋友在討論協議方面的事情,有的說 web 開發者應該熟悉 web 相關的協議,有的則說不用很了解。個人認為這要分層次來看待這個問題,對于一個新手或者剛入門的 web 開發人員而言,研究協議方面的東西可能會使得 web 開發失去趣味性、抹煞學習積極性,這類人應該更多的了解基本的 Web 技術使用。而對于在該行業工作多年的老鳥來說,協議相關的內容、標準相關內容應該盡量多些的了解,因為只有這樣才能使得經手的 web 系統更加優秀(安全、漂亮、快速、兼容性好、體驗好……)。本文我們來說一下 MIME 協議的一個擴展 Content-disposition。
我們在開發 web 系統時有時會有以下需求︰
1.希望某類或者某已知 MIME 類型的文件(比如︰*.gif;*.txt;*.htm)能夠在訪問時彈出“文件下載”對話框
2.希望以原始文件名(上傳時的文件名 , 例如︰山東省政府 1024 號文件 .doc)提供下載,但伺服器上保存的位址卻是其他文件名(如︰12519810948091234_asdf.doc)
3.希望某文件直接在瀏覽器上顯示而不是彈出文件下載對話框
……………………
要解決上述需求就可以使用 Content-disposition 來解決。第一個需求的解決辦法是
Response.AddHeader "content-disposition","attachment; filename=fname.ext"
將上述需求進行歸我給出如下例子代碼︰
public static void ToDownload(string serverfilpath,string filename)
{
FileStream fileStream = new FileStream(serverfilpath, FileMode.Open);
long fileSize = fileStream.Length;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + UTF_FileName(filename) + "\";");
attachment --- 作為附件下載
inline --- 在線打開
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
}
public static void ToOpen(string serverfilpath, string filename)
{
FileStream fileStream = new FileStream(serverfilpath, FileMode.Open);
long fileSize = fileStream.Length;
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=\"" + UTF_FileName(filename) + "\";");//直接打開是將"attachment"改為"inline"就Ok了
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
}
private static string UTF_FileName(string filename)
{
return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
}
簡單的對上述代碼做一下解析,ToDownload 方法為將一個伺服器上的文件(ServerFilpath 為伺服器上的物理位址),以某文件名(filename)在瀏覽器上彈出“文件下載”對話框,而 ToOpen 是將伺服器上的某文件以某文件名在瀏覽器中顯示/打開的。注意其中我使用了 UTF_FileName 方法,該方法很簡單,主要為了解決包含非英文/數字名稱的問題,比如說文件名為“衣明志 .doc”,使用該方法客戶端就不會出現亂碼了。
需要注意以下幾個問題︰
Content-disposition 是 MIME 協議的擴展,由於多方面的安全性考慮沒有被標準化,所以可能某些瀏覽器不支持,比如說 IE 4.01。
我們可以使用程式來使用它,也可以在 Web 伺服器(比如 IIS)上使用它,只需要在 http header 上做相應的設置即可。
下載的代碼片斷:
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.WriteFile(strFileName);
response.AppendHeader("Content-Disposition", "attachment;filename=" + UTF_FileName("小余新作.xls"));
response.Flush();
File.Delete(strFileName);
response.End();
//attachment --- 作為附件下載
//inline --- 在線打開