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

查看:284
本文介绍了文件名和MIME问题 - ASP.NET下载文件(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临在我的ASP.NET应用程序中一个很奇怪的问题。

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

当用户点击了下载文件的按钮,IE浏览器/铬/火狐显示保存对话框,但该文件的名称为ASPX页面的名称(例如,如果页面命名为Download.aspx下载对话框显示文件Download.zip)。有时,MIME类型播放时下载对话框显示Download.aspx。看来你要下载的页面,但实际上是正确的文件。

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.

这情况与ZIP扩展,这里是我的code(pretty标准我认为):

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();

该函数的getContentType仅返回MIME的文件。我试着用应用程序/ x-ZIP-COM pressed 的multipart / X-ZIP 当然的应用程序/压缩。随着应用程序/压缩Internet Explorer 8中显示XML错误。

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.

任何帮助是非常AP preciated。

Any help with be very appreciated.

问候,

推荐答案

我认为像的Response.Redirect(RESOLVEURL(file.FullName))而不是 Response.TransmitFile(file.FullName)是何意。这听起来像你真的希望自己的浏览器在文件点,而不仅仅是文件传输作为当前请求的响应。

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.

修改:另请参见这太问题<一href=\"http://stackoverflow.com/questions/1384649/how-to-retrive-and-download-server-files-file-exists-and-url\">http://stackoverflow.com/questions/1384649/how-to-retrive-and-download-server-files-file-exists-and-url

更新:根据您的意见,我认为<一href=\"http://stackoverflow.com/questions/848679/reading-a-binary-file-and-using-response-binarywrite\">this就是你在寻找什么。

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

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

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