简体   繁体   English

文件名和mime问题-ASP.NET下载文件(C#)

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

I'm facing a very strange problem in my ASP.NET Application. 我在ASP.NET应用程序中面临一个非常奇怪的问题。

When the user clicks the button that downloads a file, Internet Explorer / Chrome / Firefox shows the save dialog but the name of the file is the name of the ASPX Page (For example, if the page is named Download.aspx the download dialog shows the "file" Download.zip). 当用户单击下载文件的按钮时,Internet Explorer / Chrome / Firefox将显示“保存”对话框,但是文件名是ASPX页面的名称 (例如,如果该页面名为Download.aspx,则显示下载对话框“文件” Download.zip)。 Sometimes, when playing with MIME type the download dialog shows "Download.aspx". 有时,在使用MIME类型播放时,下载对话框会显示“ Download.aspx”。 Seems that you're trying to download the page, but actually is the correct file. 似乎您正在尝试下载页面,但实际上是正确的文件。

This happens with ZIP extension and here is my code (pretty standard I think): 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();

The GetContentType function just returns the MIME for the file. GetContentType函数仅返回文件的MIME。 I tried with application/x-zip-compressed , multipart/x-zip and of course application/zip . 我尝试了application / x-zip-compressedmultipart / x-zip ,当然还有application / zip With application/zip Internet Explorer 8 shows XML error. 使用application / zip时,Internet Explorer 8显示XML错误。

Any help with be very appreciated. 任何帮助将不胜感激。

Greetings, 问候,

I'm looking at what I've done to handle a similar mechanism, and here's the steps I'm doing (bold item seemingly the only real difference): 我正在查看为处理类似机制所做的工作,这是我正在执行的步骤(粗体似乎是唯一的真正区别):

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

Although this.Response and HttpContext.Current.Response should be the same, it may not be for some reason. 尽管this.Response和HttpContext.Current.Response应该相同,但可能出于某些原因。

I think something like Response.Redirect(ResolveUrl(file.FullName)) instead of Response.TransmitFile(file.FullName) is what you intended. 我认为您想要的是类似Response.Redirect(ResolveUrl(file.FullName))而不是Response.TransmitFile(file.FullName)的东西。 It sounds like you actually want their browser to point at the file, not just transmit the file as a response to the current the request. 听起来您实际上是希望他们的浏览器指向该文件,而不仅仅是发送文件作为对当前请求的响应。

Edit : Also see this SO question How to retrieve and download server files (File.Exists and URL) 编辑 :也请参见此SO问题如何检索和下载服务器文件(File.Exists和URL)

Update : Based on your feedback, i think this is what you're looking for. 更新 :根据您的反馈,我认为是您想要的。

For Excel export 对于Excel导出

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

It worked for me with IE and Firefox. 它适用于IE和Firefox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM