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

发布于 2024-08-07 01:33:33 字数 1007 浏览 7 评论 0原文

我在 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 错误。

非常感谢任何帮助。

问候,

I'm facing a very strange problem in my ASP.NET Application.

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). Sometimes, when playing with MIME type the download dialog shows "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):


        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. I tried with application/x-zip-compressed, multipart/x-zip and of course application/zip. With application/zip Internet Explorer 8 shows XML error.

Any help with be very appreciated.

Greetings,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临风闻羌笛 2024-08-14 01:33:33

我正在研究我为处理类似的机制所做的事情,这是我正在执行的步骤(粗体项目似乎是唯一真正的区别):

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 应该是相同的,但它可能不是因为某种原因。

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.

白首有我共你 2024-08-14 01:33:33

我认为像 Response.Redirect(ResolveUrl(file.FullName)) 而不是 Response.TransmitFile(file.FullName) 是您想要的。听起来您实际上希望他们的浏览器指向该文件,而不仅仅是传输该文件作为对当前请求的响应。

编辑:另请参阅这个问题如何检索和下载服务器文件(File.Exists 和 URL)

更新:根据您的反馈,我认为就是您要寻找的内容。

I think something like Response.Redirect(ResolveUrl(file.FullName)) instead of Response.TransmitFile(file.FullName) is what you intended. 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)

Update: Based on your feedback, i think this is what you're looking for.

能怎样 2024-08-14 01:33:33

对于 Excel 导出

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

它对我来说适用于 IE 和 Firefox。

For Excel export

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

It worked for me with IE and Firefox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文