【问题标题】:File download fails over https in IE文件下载在 IE 中通过 https 失败
【发布时间】:2011-03-04 19:50:10
【问题描述】:

我正在尝试通过 HTTPS 下载文件,它在 IE 中失败,但在 Firefox 和 Chrome 中完美运行:

aspx代码如下:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CRISIIWebApplication1.Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"  />  
</asp:Content>

点击按钮后的代码如下:

protected void Button1_Click(object sender, EventArgs e)
        {
            string filename = TextBox1.Text;
            string filepath = Server.MapPath(filename);

        byte[] bytFile = null;
        FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        long numBytes = new FileInfo(filepath).Length;
        bytFile = br.ReadBytes((int)numBytes);
        string extension = ".xlsx";

        Response.ClearHeaders();
        Response.Clear();
        Response.Buffer = true;

        if (extension == ".doc")
        {
            Response.ContentType = "application/vnd.ms-word";
            Response.AddHeader("content-disposition", "attachment;filename=" + filename);
        }

        else if (extension == ".docx")
        {
            Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            Response.AddHeader("content-disposition", "attachment;filename=" + filename);
        }

        else if (extension == ".xls" || extension == ".xlsx")
        {
            if (extension == ".xls")
            {
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("content-disposition", "attachment;filename=" + filename);
            }
            else
            {
                Response.ContentType = "application/ms-excel";
                //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=" + filename);
            }
        }
        else if (extension == ".pdf")
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + filename);
        }

        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.BinaryWrite(bytFile);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        Response.End();
    }

请帮忙

【问题讨论】:

  • 您是遇到错误,还是请求消失了?任何有关您遇到的情况的其他信息都会有所帮助。
  • 失败是什么意思?你得到什么样的错误?
  • 错误 --> Internet Explorer 无法打开此 Internet 站点。请求的站点不可用或找不到。请稍后再试。

标签: c# internet-explorer ssl https file-transfer


【解决方案1】:

正如用户 SquidScareMe 所写,通过 SSL 下载 Office 文件时,您必须忽略/不要触摸缓存设置。

我有一个 .ashx 处理程序,它有如下片段:

// "Internet Explorer is unable to open Office documents from an SSL Web site".
// http://support.microsoft.com/kb/316431/en-us
if (!context.Request.IsSecureConnection || !isInternetExplorer(context))
{
    // No cache.
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    context.Response.AppendHeader(@"Pragma", @"no-cache");
}

使用此功能:

private static bool isInternetExplorer(HttpContext context)
{
    return context.Request.Browser.Browser == @"IE";
}

【讨论】:

【解决方案2】:

http://blogs.msdn.com/b/ieinternals/archive/2009/10/03/internet-explorer-cannot-download-over-https-when-no-cache.aspx

更新:啊哈! http://www.openrdf.org/issues/browse/SES-63

解决方案: Internet Explorer-> 工具菜单-> Internet 选项-> 高级选项卡 一直转到底部的安全部分。 取消勾选“不要将加密的页面保存到磁盘” 关闭所有 Internet Explorer 窗口 启动 IE 并重新下载文件

【讨论】:

  • 在我看来,这不是一个解决方案,因为用户必须进行一些客户端调整。更好的方法是不通过 SSL 缓存 Office 文件。
【解决方案3】:

解决此问题的解决方法是在 ISA 上激活压缩。完成此步骤后,网站可以毫无问题地传输文件! 当您尝试在使用无缓存时通过 HTTPS 传输文件时会出现此问题。

【讨论】:

    【解决方案4】:

    您可以通过指定 Cache-Control 标头来解决此问题,如下所示:

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

    这样你仍然可以指定你的缓存设置,它可以与 https 一起使用。

    见:http://blogs.msdn.com/b/ieinternals/archive/2009/10/03/internet-explorer-cannot-download-over-https-when-no-cache.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多