【问题标题】:Why does IE fail to server my CSV file from Response.Write?为什么 IE 无法从 Response.Write 提供我的 CSV 文件?
【发布时间】:2011-05-07 05:38:42
【问题描述】:

我最近开发了一个列出一系列报告的网络用户控件。当用户点击报告时,它会使用以下代码在响应流中返回一个 CSV 文件下载:

 Response.Clear();

 Response.ContentType = "text/CSV";

 Response.CacheControl = "no-cache";
 Response.AddHeader("Pragma", "no-cache");
 Response.Expires = -1;

 Response.AddHeader("Pragma", "must-revalidate");
 Response.AddHeader("Cache-Control", "must-revalidate");
 Response.AddHeader("Accept-Header", csvResults.Length.ToString());
 Response.AddHeader("Content-Length", csvResults.Length.ToString());
 Response.AddHeader("content-disposition", "attachment; filename=test.csv");
 Response.Write(csvResults.ToString());
 Response.Flush();
 Response.End();

代码最初在所有浏览器中都能正常工作。然后客户提出要求为该站点使用 SSL。作为其中的一部分,我引入了一个全局处理程序,用于将所有请求的协议从 HTTP 更新为 HTTPS,如下所示:

protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        string requestURL = Request.Url.ToString().ToLower();
        if (requestURL.StartsWith("http://"))
        {
            Response.Redirect(requestURL.Replace("http:", "https:"));
        }
    }

但是,由于使用 SSL 保护站点,CSV 文件下载不再适用于 IE,尽管它们继续适用于 Firefox / Chrome / Safari。

为了使文件响应正常工作,我在 IE 的标头中是否缺少某些东西?

我从 IE 收到的消息是:

“Internet Explorer 无法下载 Reports.aspx 来自 .... in ......

Internet Explorer 无法打开 这个互联网站点。请求的站点 不可用或不能 成立。请稍后再试。”

更新:

这是一些从页面请求返回的 fiddler 输出示例,看起来它服务正常。为什么 IE 不明白它只是被提供了一个文件?

HTTP/1.1 200 OK
Date: Tue, 09 Nov 2010 14:23:50 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Pragma: no-cache
Pragma: must-revalidate

content-disposition: attachment; filename="test.csv"
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Length: <length value would be here>
Content-Type: text/CSV

"COL1","COL2","COL3"
"VAL1","VAL2","VAL3"
"VAL1","VAL2","VAL3"
"VAL1","VAL2","VAL3"

【问题讨论】:

    标签: asp.net internet-explorer header download content-type


    【解决方案1】:

    不确定这是否能解决您的问题,但我会仔细检查对 csv 文件的请求最初是使用 https 发出的,以避免在 Application_BeginRequest 中进行重定向。

    您可以使用Fiddler 查看所有发出的请求,这会告诉您是否点击了重定向代码。

    【讨论】:

    • 在请求启动之前,页面肯定是 HTTPS,fiddler 确认。
    • 嗯。点击报告时的 url 是什么样的?它是提供 csv 文件的页面的相对路径吗?
    • 这是返回响应的同一页面。每个报告都有一个链接,当用户单击该链接时,上面的第一个示例代码块将执行,并且 csvResults 变量中存在正确的 csv 数据。
    【解决方案2】:

    花了几个小时试图解决这个问题后,我终于想出了一个解决方案。

    幸运的是,我设法遇到了 a post by Eric Law 关于 IE 在 HTTPS 和包含缓存指令的响应标头之间的不兼容问题。

    现在确保所有响应标头都已清除并且没有对响应执行缓存指令,然后文件开始再次响应下载。

    【讨论】:

      【解决方案3】:

      正如 Brian 所说,Eric Law 的帖子是解开这个谜团的关键……

      快速解决方案是:

      Response.ClearHeaders();
      Response.AddHeader("Cache-Control", "no-store, no-cache");
      

      (无法解释的事实是,“no-store”必须“no-cache”之前)

      【讨论】:

      • 第二行是我的灵丹妙药。谢谢
      【解决方案4】:

      在 IIS 7.5+ 中,使用URL Rewrite extention 添加出站规则以去除 Cache-Control 标头中的“no-store”值,并去除 Pragma 标头(从不需要)。这个规则集可以解决问题:

      <outboundRules>
          <rule name="Always Remove Pragma Header">
              <match serverVariable="RESPONSE_Pragma" pattern="(.*)" />
              <action type="Rewrite" value="" />
          </rule>
          <rule name="Remove No-Store for Attachments">
              <conditions>
                  <add input="{RESPONSE_Content-Disposition}" pattern="attachment" />
              </conditions>
              <match serverVariable="RESPONSE_Cache-Control" pattern="no-store" />
              <action type="Rewrite" value="max-age=0" />
          </rule>
      </outboundRules>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-10
        • 2021-10-24
        • 2013-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多