簡體   English   中英

ASP.NET中IE 7,8中“另存為”對話框的問題

[英]Problem with Save As dialog in IE 7,8 in ASP.NET

我正在嘗試編寫代碼,以下載位於服務器上的文件。 但是另存為對話框不會在IE中打開。

我嘗試了response.redirect,我嘗試了

 Response.Clear();
 Response.ContentType = "text/csv"; 
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay); 
 Response.WriteFile(Server.MapPath(pathName + fileNameUnique)); 
 Response.Flush(); 
 Response.End(); 

一切都適用於firefox和chrome,但不適用於Internet Explorer。 我知道在安全性中有一個安全選項--->自定義級別--->下載--->自動提示文件下載,始終處於禁用模式,我需要將其切換為啟用才能進行上班,但我不會讓我的用戶來處理。 我該如何克服這個“安全問題”?

有什么正確的方法來處理下載文件嗎? 什么是正確的代碼做到這一點?

謝謝你,蓋迪

您需要明確告知它未打開的內容,添加以下標頭:

Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);
Response.AppendHeader("Content-Transfer-Encoding", "binary");

另外,強烈建議設置顯式文件長度

Response.AppendHeader("Content-Length", responseContentLength.ToString());
var info = new FileInfo(path);

Response.Clear();

Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", info.Name));
Response.AppendHeader("Content-Length", info.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "text/csv";

Response.WriteFile(info.FullName, true);
Response.End();

abatishchev,謝謝你對我的幫助。 我為我的問題感到高​​興。

我創建了一個對話框窗口(我們稱其為“ DownloadWindow”),其中包含一個空的“ A HREF”標簽。 (顯示-'點擊此處下載')

單擊默認頁面上的下載(EXCEL / CSV圖標)按鈕(動態創建文件)后,我加載對話框窗口(“ DownloadWindow”),然后重新添加指向文件網址的“ a href”鏈接我之前創建過,因此我的用戶可以從我的服務器下載它。

現在,Internet Explorer確實彈出了“打開/保存/取消”對話框。

這有點煩人,但解決了我的問題。

使用以下文件進行文件下載鏈接時,我遇到了同樣的問題:

<a href="DownloadFile.aspx">
    <input type="image" src="~/virtual/path/to/image.png" runat="server" />
</a>

當我將其更改為:問題消失了

<a href="DownloadFile.aspx">
    <img src="~/virtual/path/to/image.png" runat="server" />
</a>

暫無
暫無

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

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