- response.setContentType( "application/pdf" ); // MIME type for pdf doc
- response.setHeader("Content-Disposition","attachment;filename=output.pdf;");
- switch (df){
- case xls:
- // contentType設(shè)定
- contentType = "application/vnd.ms-excel;charset=utf-8";
- // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框
- filename = "inline; filename=" + fileName;
- break;
- case xlsx:
- // contentType設(shè)定
- contentType = "application/vnd.ms-excel;charset=utf-8";
- // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框
- filename = "inline; filename=" + fileName;
- break;
- case pdf:
- // contentType設(shè)定
- contentType = "application/pdf;charset=utf-8";
- // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框
- filename = "inline; filename=" + fileName;
- break;
- case doc:
- // contentType設(shè)定
- contentType = "application/msword;charset=utf-8";
- // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框
- filename = "inline; filename=" + fileName;
- break;
- case docx:
- // contentType設(shè)定
- contentType = "application/msword;charset=utf-8";
- // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框
- filename = "inline; filename=" + fileName;
- break;
- case txt:
- // contentType設(shè)定
- contentType = "text/plain;charset=utf-8";
- // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框
- filename = "inline; filename=" + fileName;
- break;
- default:
- // contentType設(shè)定
- contentType = "text/plain;charset=utf-8";
- // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框
- filename = "inline; filename=" + fileName;
- break;
- }
最近不少Web技術(shù)圈內(nèi)的朋友在討論協(xié)議方面的事情,有的說(shuō)web開(kāi)發(fā)者應(yīng)該熟悉web相關(guān)的協(xié)議,有的則說(shuō)不用很了解。個(gè)人認(rèn)為這要分層次來(lái)看待這個(gè)問(wèn) 題,對(duì)于一個(gè)新手或者剛?cè)腴T(mén)的web開(kāi)發(fā)人員而言,研究協(xié)議方面的東西可能會(huì)使得web開(kāi)發(fā)失去趣味性、抹煞學(xué)習(xí)積極性,這類人應(yīng)該更多的了解基本的 Web技術(shù)使用。而對(duì)于在該行業(yè)工作多年的老鳥(niǎo)來(lái)說(shuō),協(xié)議相關(guān)的內(nèi)容、標(biāo)準(zhǔn)相關(guān)內(nèi)容應(yīng)該盡量多些的了解,因?yàn)橹挥羞@樣才能使得經(jīng)手的web系統(tǒng)更加優(yōu)秀 (安全、漂亮、快速、兼容性好、體驗(yàn)好……)。本文我們來(lái)說(shuō)一下MIME 協(xié)議的一個(gè)擴(kuò)展Content-disposition。
我們?cè)陂_(kāi)發(fā)web系統(tǒng)時(shí)有時(shí)會(huì)有以下需求:
- 希望某類或者某已知MIME 類型的文件(比如:*.gif;*.txt;*.htm)能夠在訪問(wèn)時(shí)彈出“文件下載”對(duì)話框
- 希望以原始文件名(上傳時(shí)的文件名,例如:山東省政府1024號(hào)文件.doc)提供下載,但服務(wù)器上保存的地址卻是其他文件名(如:12519810948091234_asdf.doc)
- 希望某文件直接在瀏覽器上顯示而不是彈出文件下載對(duì)話框
- ……………………
要解決上述需求就可以使用Content-disposition來(lái)解決。第一個(gè)需求的解決辦法是
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 --- 在線打開(kāi)
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) + """;");
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);
}
簡(jiǎn)單的對(duì)上述代碼做一下解析,ToDownload方法為將一個(gè)服務(wù)器上的文件(serverfilpath為服務(wù)器上的物理地址),以某文件名 (filename)在瀏覽器上彈出“文件下載”對(duì)話框,而ToOpen是將服務(wù)器上的某文件以某文件名在瀏覽器中顯示/打開(kāi)的。注意其中我使用了 UTF_FileName方法,該方法很簡(jiǎn)單,主要為了解決包含非英文/數(shù)字名稱的問(wèn)題,比如說(shuō)文件名為“衣明志.doc”,使用該方法客戶端就不會(huì)出現(xiàn) 亂碼了。
需要注意以下幾個(gè)問(wèn)題:
- Content-disposition是MIME協(xié)議的擴(kuò)展,由于多方面的安全性考慮沒(méi)有被標(biāo)準(zhǔn)化,所以可能某些瀏覽器不支持,比如說(shuō)IE4.01
- 我們可以使用程序來(lái)使用它,也可以在web服務(wù)器(比如IIS)上使用它,只需要在http header上做相應(yīng)的設(shè)置即可
可參看以下幾篇文檔:
- 如何為已知的 MIME 類型激活“文件下載”對(duì)話框
- Communicating Presentation Information in Internet Messages: The Content-Disposition Header Field
- Hypertext Transfer Protocol -- HTTP/1.1
response.setContentType("text/html; charset=Shift_JIS");
- HTML format (.htm or .html): text/html
- Adobe Portable Document (pdf): application/pdf
- Microsoft Word (.doc): application/msword
- Microsoft Excel (.xls): application/msexcel
- Microsoft Powerpoint (.ppt): application/ms-powerpoint
- Realaudio (.rm, .ram): audio/x-pn-realaudio
- Text format (.txt): text/txt
- Zipped files (.zip): application/zip