Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評(píng)論 :: 0 Trackbacks
          java 代碼
           
          1. response.setContentType( "application/pdf" );  // MIME type for pdf doc  
          2. response.setHeader("Content-Disposition","attachment;filename=output.pdf;");  

          1. switch (df){  
          2.         case xls:  
          3.             // contentType設(shè)定      
          4.             contentType = "application/vnd.ms-excel;charset=utf-8";      
          5.             // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框      
          6.             filename = "inline; filename=" + fileName;      
          7.             break;                
          8.         case xlsx:  
          9.             // contentType設(shè)定      
          10.             contentType = "application/vnd.ms-excel;charset=utf-8";      
          11.             // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框      
          12.             filename = "inline; filename=" + fileName;      
          13.             break;  
          14.         case pdf:    
          15.             // contentType設(shè)定      
          16.             contentType = "application/pdf;charset=utf-8";      
          17.             // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框      
          18.             filename = "inline; filename=" + fileName;   
          19.             break;  
          20.         case doc:  
          21.             // contentType設(shè)定      
          22.             contentType = "application/msword;charset=utf-8";      
          23.             // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框      
          24.             filename = "inline; filename=" + fileName;      
          25.             break;                
          26.         case docx:  
          27.             // contentType設(shè)定      
          28.             contentType = "application/msword;charset=utf-8";      
          29.             // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框      
          30.             filename = "inline; filename=" + fileName;      
          31.             break;  
          32.         case txt:  
          33.             // contentType設(shè)定      
          34.             contentType = "text/plain;charset=utf-8";      
          35.             // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框      
          36.             filename = "inline; filename=" + fileName;      
          37.             break;  
          38.         default:  
          39.             // contentType設(shè)定      
          40.             contentType = "text/plain;charset=utf-8";      
          41.             // attachment表示網(wǎng)頁(yè)會(huì)出現(xiàn)保存、打開(kāi)對(duì)話框      
          42.             filename = "inline; filename=" + fileName;    
          43.             break;  
          44.     }    


          Content-Disposition的使用和注意事項(xiàng)

              最近不少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"
           
          將上述需求進(jìn)行歸我給出如下例子代碼:
          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)題:

          1. Content-disposition是MIME協(xié)議的擴(kuò)展,由于多方面的安全性考慮沒(méi)有被標(biāo)準(zhǔn)化,所以可能某些瀏覽器不支持,比如說(shuō)IE4.01
          2. 我們可以使用程序來(lái)使用它,也可以在web服務(wù)器(比如IIS)上使用它,只需要在http header上做相應(yīng)的設(shè)置即可

          可參看以下幾篇文檔:



          Sets the Content-Type header. Content-Type identifies the MIME type of the response document and the character set encoding. To set the Content-Type header, use the following code:
          response.setContentType("text/html; charset=Shift_JIS");







          The default MIME type is text/plain. Some common MIME types include:
          • 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


          posted on 2010-01-25 21:31 seal 閱讀(1272) 評(píng)論(0)  編輯  收藏 所屬分類: web
          主站蜘蛛池模板: 油尖旺区| 丽水市| 怀宁县| 郴州市| 兴山县| 体育| 鄄城县| 屯门区| 滦平县| 临泽县| 徐水县| 宿迁市| 许昌市| 宜昌市| 革吉县| 沁水县| 德令哈市| 墨竹工卡县| 海淀区| 原平市| 德钦县| 泸水县| 潍坊市| 嘉义市| 济源市| 崇礼县| 神农架林区| 陆河县| 宁化县| 新疆| 嵩明县| 章丘市| 平果县| 容城县| 文成县| 宁南县| 墨竹工卡县| 平阴县| 鄱阳县| 公安县| 普宁市|