文件名和 mime 问题 - ASP.NET 下载文件 (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-compressed、multipart/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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在研究我为处理类似的机制所做的事情,这是我正在执行的步骤(粗体项目似乎是唯一真正的区别):
虽然 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):
Although this.Response and HttpContext.Current.Response should be the same, it may not be for some reason.
我认为像
Response.Redirect(ResolveUrl(file.FullName))
而不是Response.TransmitFile(file.FullName)
是您想要的。听起来您实际上希望他们的浏览器指向该文件,而不仅仅是传输该文件作为对当前请求的响应。编辑:另请参阅这个问题如何检索和下载服务器文件(File.Exists 和 URL)
更新:根据您的反馈,我认为这就是您要寻找的内容。
I think something like
Response.Redirect(ResolveUrl(file.FullName))
instead ofResponse.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.
对于 Excel 导出
它对我来说适用于 IE 和 Firefox。
For Excel export
It worked for me with IE and Firefox.