簡體   English   中英

Response.AddHeader(“Content-Disposition”)不在IE6中打開文件

[英]Response.AddHeader(“Content-Disposition”) not opening file in IE6

我正在使用Response.AddHeader(“Content-Disposition”,“attachment; filename =”+ Server.HtmlEncode(FileName)); 為用戶彈出“打開/保存文件”對話框,以便他們可以將文件下載到本地計算機上。

這通常在IE7中運行良好,但在IE6上,當用戶單擊“打開/保存文件”對話框中的打開按鈕時,文件無法打開。 我瀏覽了網絡,發現了Response.AddHeader(“Content-Disposition”,“inline; filename =”+ Server.HtmlEncode(FileName)); 應該提供IE6中的工作,它的工作正常..

但問題是大多數可以在瀏覽器中打開的文件在頁面本身打開..即用戶在郵件頁面上並單擊下載它打開的圖像文件,我需要它在IE7的情況下在另一個窗口中打開我該怎么做...其他文件無法在系統中使用當前應用程序打開,即(word,excel等)..

請建議一種方法,堅持使用相同的代碼為兩個IE ...我使用的代碼在這里....

Response.AddHeader("Content-Disposition", "attachment; filename=" +FileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.TransmitFile(file.FullName);
Response.End();

 private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".xls":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            default:
                return "application/octet-stream";
        }
    }

我在IE 6中發現了問題我們必須在IE 6中讀取內容並使用緩沖區和二進制寫入打開文件,下面的代碼在IE6中對我來說很好用

FileStream sourceFile = new FileStream(Server.MapPath(@"FileName"), FileMode.Open);
float FileSize;
FileSize = sourceFile.Length;
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.AddHeader("Content-Length", getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.BinaryWrite(getContent);
Response.Flush();
Response.End();

嘗試將此內容類型設置為八位字節流:

Response.ContentType = "application/octet-stream";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM