簡體   English   中英

文件名和mime問題-ASP.NET下載文件(C#)

[英]Filename and mime problems - ASP.NET Download file (C#)

我在ASP.NET應用程序中面臨一個非常奇怪的問題。

當用戶單擊下載文件的按鈕時,Internet Explorer / Chrome / Firefox將顯示“保存”對話框,但是文件名是ASPX頁面的名稱 (例如,如果該頁面名為Download.aspx,則顯示下載對話框“文件” Download.zip)。 有時,在使用MIME類型播放時,下載對話框會顯示“ Download.aspx”。 似乎您正在嘗試下載頁面,但實際上是正確的文件。

ZIP擴展會發生這種情況,這是我的代碼(我認為是相當標准的):


        this.Response.Clear();
        this.Response.ClearHeaders();
        this.Response.ClearContent();
        this.Response.AddHeader("Content–Disposition", "attachment; filename=" + file.Name);
        this.Response.AddHeader("Content-Length", file.Length.ToString());
        this.Response.ContentType = GETCONTENTYPE(System.IO.Path.GetExtension(file.Name));
        this.Response.TransmitFile(file.FullName);
        this.Response.End();

GetContentType函數僅返回文件的MIME。 我嘗試了application / x-zip-compressedmultipart / x-zip ,當然還有application / zip 使用application / zip時,Internet Explorer 8顯示XML錯誤。

任何幫助將不勝感激。

問候,

我正在查看為處理類似機制所做的工作,這是我正在執行的步驟(粗體似乎是唯一的真正區別):

Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Excel 2007 format
// ... doing work...
Response.AddHeader("Content-Length", outputFileInfo.Length.ToString());
Response.TransmitFile(outputFileInfo.ToString());
HttpContext.Current.Response.End(); // <--This seems to be the only major difference

盡管this.Response和HttpContext.Current.Response應該相同,但可能出於某些原因。

我認為您想要的是類似Response.Redirect(ResolveUrl(file.FullName))而不是Response.TransmitFile(file.FullName)的東西。 聽起來您實際上是希望他們的瀏覽器指向該文件,而不僅僅是發送文件作為對當前請求的響應。

編輯 :也請參見此SO問題如何檢索和下載服務器文件(File.Exists和URL)

更新 :根據您的反饋,我認為是您想要的。

對於Excel導出

   Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));

它適用於IE和Firefox。

暫無
暫無

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

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