【问题标题】:ASP.NET MVC cannot stream filesASP.NET MVC 无法流式传输文件
【发布时间】:2014-07-24 21:47:50
【问题描述】:

我有一个 ASP.NET MVC 4 站点,它使用 OPEN XML SDK 创建一个 excel 文件。我只是将超链接指向正确的控制器,它会生成 OPEN XML excel 文档并将流写入响应头并完成。在 IE 9 和 Chrome 中,这可以正常工作。使用给定的文件名和正确的内容下载文件。但是,就在最近,我将浏览器升级到 IE 10,现在我没有下载文件并在 excel 中打开,而是收到 could not open 'URI' 的错误。当我单击确定时,它会出现另一个错误:Microsoft Excel cannot access the file 'URI'. There are several possible reasons:

我不明白为什么这在 IE 9 和 chrome 中有效,而在 IE 10 中无效。我用提琴手调试了响应标头,它具有正确的内容类型和内容长度设置:

Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Content Disposition: attachment; filename=result.xlsx

Content length: 1232

我有什么遗漏的吗?

代码 sn-p:这都是

的一部分
public override void ExecuteResult(ControllerContext context)
{
...
....
..
    extention = "xlsx";
    response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    response.AddHeader("Content-Disposition",
                                    String.Format("attachment; filename={0}.{1}", fileName, extention));
    response.AddHeader("Content-Length", mem.Length.ToString());
    mem.Seek(0, SeekOrigin.Begin); // Go back to the begining.
    mem.CopyTo(response.OutputStream);
    context.HttpContext.ApplicationInstance.CompleteRequest();
}

【问题讨论】:

  • 您是否通过 SSL 传送内容?另外,它列出了几个可能的原因?
  • writes the stream to response header and done,你能显示代码吗?
  • 请包含您正在使用的代码的最小示例
  • 请显示一些代码...这个答案为我解决了类似的问题:stackoverflow.com/questions/16655566/ie-10-file-download-issues
  • @xDaevax:我在没有 SSL 的情况下看到了同样的问题。我没有说任何可能的原因。

标签: c# asp.net-mvc-4 internet-explorer internet-explorer-10 openxml-sdk


【解决方案1】:

作为一种解决方法,Controller.File 适用于我的 ASP.NET MVC 4 和 IE 10:

public class DownloadController : Controller
{
    public ActionResult GetFile()
    {
        ...

        mem.Position = 0;

        return File(
            mem, 
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
            "result.xlsx");
    }
}

编辑

所以我一直在 CodePlex 浏览 MVC 源代码,发现了一些与您的代码 sn-p 不同的实现细节:

  • Content-Length 标头从未设置

  • 他们没有调用Stream.CopyTo,而是使用了一个简单的Stream.Read、Stream.Write循环,缓冲区大小为0x1000

  • 在设置Content-Disposition标头时,他们检查文件下载名称是否包含UTF-8字符,如果是,则根据RFC 2231对其进行编码。参见ContentDispositionUtil.GetHeaderValue

  • context.HttpContext.ApplicationInstance.CompleteRequest() 永远不会被调用

现在您可以尝试一一应用这些更改,看看哪一个使其适用于 IE 10。

【讨论】:

  • 虽然我也试过这个,但无法让它工作。我接受这个作为答案,因为这是帮助我的最佳尝试。
【解决方案2】:

我猜这是一个 IE10 错误。但也许你仍然可以解决它。

基于this post 和其他一些。 请检查:

  1. Content Disposition 标头的完整 URI 和文件名均不包含特殊符号,并且它们的长度小于 105。例如,只需使用常规 A-Za-z0-9 符号即可。

  2. 尝试添加具有“IE=9”值的“X-UA-Compatible”HTTP 标头。

【讨论】:

【解决方案3】:

试试这些 MIME 类型:- 对于旧版本:-

Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment; filename=myfile.xls");

2007 年:-

Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-disposition", "attachment; filename=result.xlsx");

对于需要在excel中打开的csv文件是:-

Response.AddHeader "Content-Disposition", "Attachment;Filename=myfile.csv"

最后你可以试试:-

Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "attachment; filename=result.xlsx");

谢谢

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 1970-01-01
    • 2017-11-20
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多