<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

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

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 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設定      
    4.             contentType = "application/vnd.ms-excel;charset=utf-8";      
    5.             // attachment表示網(wǎng)頁會出現(xiàn)保存、打開對話框      
    6.             filename = "inline; filename=" + fileName;      
    7.             break;                
    8.         case xlsx:  
    9.             // contentType設定      
    10.             contentType = "application/vnd.ms-excel;charset=utf-8";      
    11.             // attachment表示網(wǎng)頁會出現(xiàn)保存、打開對話框      
    12.             filename = "inline; filename=" + fileName;      
    13.             break;  
    14.         case pdf:    
    15.             // contentType設定      
    16.             contentType = "application/pdf;charset=utf-8";      
    17.             // attachment表示網(wǎng)頁會出現(xiàn)保存、打開對話框      
    18.             filename = "inline; filename=" + fileName;   
    19.             break;  
    20.         case doc:  
    21.             // contentType設定      
    22.             contentType = "application/msword;charset=utf-8";      
    23.             // attachment表示網(wǎng)頁會出現(xiàn)保存、打開對話框      
    24.             filename = "inline; filename=" + fileName;      
    25.             break;                
    26.         case docx:  
    27.             // contentType設定      
    28.             contentType = "application/msword;charset=utf-8";      
    29.             // attachment表示網(wǎng)頁會出現(xiàn)保存、打開對話框      
    30.             filename = "inline; filename=" + fileName;      
    31.             break;  
    32.         case txt:  
    33.             // contentType設定      
    34.             contentType = "text/plain;charset=utf-8";      
    35.             // attachment表示網(wǎng)頁會出現(xiàn)保存、打開對話框      
    36.             filename = "inline; filename=" + fileName;      
    37.             break;  
    38.         default:  
    39.             // contentType設定      
    40.             contentType = "text/plain;charset=utf-8";      
    41.             // attachment表示網(wǎng)頁會出現(xiàn)保存、打開對話框      
    42.             filename = "inline; filename=" + fileName;    
    43.             break;  
    44.     }    


    Content-Disposition的使用和注意事項

        最近不少Web技術圈內的朋友在討論協(xié)議方面的事情,有的說web開發(fā)者應該熟悉web相關的協(xié)議,有的則說不用很了解。個人認為這要分層次來看待這個問 題,對于一個新手或者剛入門的web開發(fā)人員而言,研究協(xié)議方面的東西可能會使得web開發(fā)失去趣味性、抹煞學習積極性,這類人應該更多的了解基本的 Web技術使用。而對于在該行業(yè)工作多年的老鳥來說,協(xié)議相關的內容、標準相關內容應該盡量多些的了解,因為只有這樣才能使得經(jīng)手的web系統(tǒng)更加優(yōu)秀 (安全、漂亮、快速、兼容性好、體驗好……)。本文我們來說一下MIME 協(xié)議的一個擴展Content-disposition。

        我們在開發(fā)web系統(tǒng)時有時會有以下需求:

    • 希望某類或者某已知MIME 類型的文件(比如:*.gif;*.txt;*.htm)能夠在訪問時彈出“文件下載”對話框
    • 希望以原始文件名(上傳時的文件名,例如:山東省政府1024號文件.doc)提供下載,但服務器上保存的地址卻是其他文件名(如:12519810948091234_asdf.doc)
    • 希望某文件直接在瀏覽器上顯示而不是彈出文件下載對話框
    • ……………………

        要解決上述需求就可以使用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) + """;");

    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方法,該方法很簡單,主要為了解決包含非英文/數(shù)字名稱的問題,比如說文件名為“衣明志.doc”,使用該方法客戶端就不會出現(xiàn) 亂碼了。

     需要注意以下幾個問題:

    1. Content-disposition是MIME協(xié)議的擴展,由于多方面的安全性考慮沒有被標準化,所以可能某些瀏覽器不支持,比如說IE4.01
    2. 我們可以使用程序來使用它,也可以在web服務器(比如IIS)上使用它,只需要在http header上做相應的設置即可

    可參看以下幾篇文檔:



    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 閱讀(1275) 評論(0)  編輯  收藏 所屬分類: web
    主站蜘蛛池模板: 亚洲日本精品一区二区| 亚洲av手机在线观看| 久久亚洲精品AB无码播放| 一级午夜a毛片免费视频| 国产一区二区三区免费视频| 亚洲人成人伊人成综合网无码| 啦啦啦中文在线观看电视剧免费版 | 成年免费a级毛片| 日韩免费视频播播| 亚洲精品乱码久久久久蜜桃| 成人一a毛片免费视频| 亚洲熟妇无码av另类vr影视| 成人五级毛片免费播放| 亚洲人成人无码.www石榴 | 0588影视手机免费看片| 337p日本欧洲亚洲大胆精品555588 | 美女又黄又免费的视频| 国产精品酒店视频免费看| 亚洲欧美精品午睡沙发| 国产一级淫片视频免费看| 美女黄频a美女大全免费皮| 亚洲午夜福利精品久久| 久操视频在线免费观看| 亚洲熟妇色自偷自拍另类| 天天干在线免费视频| 成年免费a级毛片| 亚洲人成影院在线| 无码一区二区三区AV免费| 国产亚洲精品国产福利在线观看| 亚洲人成网站色在线入口| 免费一级毛片在线播放视频| 亚洲福利秒拍一区二区| 国产18禁黄网站免费观看| 中文字幕乱码免费看电影| 亚洲午夜国产精品无卡| 国产免费人成在线视频| 永久免费av无码网站yy| 国产午夜亚洲精品| 国产aⅴ无码专区亚洲av麻豆| 亚洲网站免费观看| 免费很黄无遮挡的视频毛片|