使用ASP MVC 4.0 FileResult Internet Explorer错误 [英] Internet Explorer error using Asp MVC 4.0 FileResult

查看:150
本文介绍了使用ASP MVC 4.0 FileResult Internet Explorer错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,部署在HTTPS Asp位,建立与MVC 4.0:

I have the following code, deployed on a https Asp site, build with MVC 4.0:

public FileResult ANotSoWorkingFunction(string filePath, string fileName)
{
 pathToFile = string.Format("~/{0}/{1}", pathToFile, fileName);
 return File(new FileStream(pathToFile, FileMode.Open), "application/pdf", fileName);
}

这将工作(如你许多你可能已经猜到)使用Chrome,火狐和IE9。但它会抛出一个:

This will work (as you many of you probably already guessed) with Chrome, Firefox and IE9. But it will throw a:

---------------------------
Windows Internet Explorer
---------------------------
Internet Explorer cannot download someFileName from a_site.com.


Internet Explorer was not able to open this Internet site.  The requested site is either unavailable or cannot be found.  Please try again later.
---------------------------
OK   
---------------------------

在IE6,7,8

在这一个任何想法或线索大大AP preciated因为我已经花洞一天HTML头打。

Any ideas or clues on this one are greatly appreciated as I already spend the hole day playing with html header.

编辑:

下面是头球IE7:

HTTP/1.1 200 OK
Cache-Control: private, no-cache="Set-Cookie"
Content-Type: application/pdf
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXAUTH=; expires=Mon, 11-Oct-1999 21:00:00 GMT; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Wed, 04 Apr 2012 08:43:50 GMT
Content-Length: 233324

这是那些自IE9:

HTTP/1.1 200 OK
Cache-Control: private, no-cache="Set-Cookie"
Content-Type: application/pdf
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXAUTH=; expires=Mon, 11-Oct-1999 21:00:00 GMT; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Wed, 04 Apr 2012 08:42:14 GMT
Content-Length: 233324

感谢您,

推荐答案

我想我也碰到了你的问题。

I think I also ran into your problem.

我也运行IIS 7.5,并通过HTTPS请求的动作下载PDF。至于原因,我还没有隔离,IIS 7.5似乎是追加无缓存=设置Cookie我的的Cache-Control 响应头,无论什么我设置缓存设置的响应。这是造成于IE6,IE7中相当有据可查无缓存的问题, IE8

I am also running IIS 7.5 and downloading a PDF through an action on an HTTPS request. For reasons I have yet to isolate, IIS 7.5 seems to be appending no-cache="Set-Cookie" to my Cache-Control response header regardless of what I set the Cache settings to on the Response. This was causing the fairly well documented no-cache issue on IE6, IE7, and IE8.

要解决这个问题,我做了周围的清除头的FileContentResult的小包装,称为父,那么可缓存设置为私人。此方 - 加强IIS 7.5的坚持加入无缓存=设置Cookie来的头,并在我测试的所有浏览器上正常下载的文件。如果你想模仿我做什么,第一,这是我FileContentResult包装。

To resolve this, I made a small wrapper around the FileContentResult that cleared the headers, called the parent, then set the Cacheability to 'Private'. This side-stepped IIS 7.5's insistence to add no-cache="Set-Cookie" to the header, and the file downloaded properly in all browsers I tested. If you want to emulate what I did, first, here's my FileContentResult wrapper.

public class PdfContentResult : FileContentResult {

    public PdfContentResult(byte[] data) : base(data, "application/pdf") { }

    public PdfContentResult(byte[] data, string fileName) : this(data) {
        if (fileName == null) {
            throw new ArgumentNullException("fileName");
        }

        this.FileDownloadName = fileName;
    }

    public override void ExecuteResult(ControllerContext context) {
        context.HttpContext.Response.ClearHeaders();

        base.ExecuteResult(context);

        context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Private);
    }
}

然后,我添加一个扩展方法我 ControllerExtensions 所以,这将是简单地发现:

Then I added an extension method to my ControllerExtensions so that it would be simple to find:

public static class ControllerExtensions {

    public static PdfContentResult Pdf(this Controller controller, byte[] fileContents, string fileName) {
        return new PdfContentResult(fileContents, fileName);
    }

}

最后,在操作中,我做了这个等价的:

Finally, within the Action, I did the equivalent of this:

public ActionResult MyGeneratedPdf() {
    byte[] myPdfContentInByteStream = GetPdfFromModel();
    return this.Pdf(myPdfContentInByteStream, "MyFile.pdf");
}

显然,如果你下载所有类型的数据类型,你可能不想解决方法如此紧密地结合到PDF。

Obviously, if you're downloading all kinds of data types, you might not want to bind the workaround so closely to PDF.

这篇关于使用ASP MVC 4.0 FileResult Internet Explorer错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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