文件名和MIME问题 - ASP.NET下载文件(C#) [英] Filename and mime problems - ASP.NET Download file (C#)

查看:148
本文介绍了文件名和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 。使用应用程序/ zip Internet Explorer 8显示XML错误。



任何帮助非常感谢。



解决方案

我觉得像 Response.Redirect(ResolveUrl(file.FullName))而不是 Response.TransmitFile(file.FullName)是您的意图。听起来你真的希望他们的浏览器指向该文件,而不仅仅是将文件作为响应发送到当前的请求。



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



更新:根据您的反馈,我认为这是您正在寻找的。

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,

解决方案

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.

这篇关于文件名和MIME问题 - ASP.NET下载文件(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆