在 IE 中通过 https 下载文件失败

发布于 2024-10-20 15:32:06 字数 2815 浏览 2 评论 0原文

我正在尝试通过 HTTPS 下载文件它在 IE 中失败,但在 Firefox 和 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();
    }

请帮忙

I'm trying to download a file over HTTPS & it fails in IE but works perfectly with Firefox & Chrome:

aspx code is as follows:

<%@ 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>

Code behind code on button click is as follows:

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

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

预谋 2024-10-27 15:32:06

正如用户 SquidScareMe 所写,通过 SSL 下载 Office 文件时,您必须忽略/不要触及 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";
}

As user SquidScareMe writes, you have to ignore/don't touch the cache settings for Office files when downloading them over SSL.

I have an .ashx handler which has a fragment like:

// "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");
}

With this function:

private static bool isInternetExplorer(HttpContext context)
{
    return context.Request.Browser.Browser == @"IE";
}
德意的啸 2024-10-27 15:32:06

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 Explorer 窗口
启动 IE 并再次下载文件

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

Update: Ahah!
http://www.openrdf.org/issues/browse/SES-63

SOLUTION:
Internet Explorer-> Tools menu-> Internet Options-> Advanced tab
Go to the Security section all the way at the bottom.
Clear the check on the "Do not save encrypted pages to disk"
Close all Internet Explorer windows
Start IE and download the file again

长安忆 2024-10-27 15:32:06

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

The work around solution for this problem is to activate compression at ISA. After this step the web site can transmit files without any problem!
The problem occurs when you try to transmit a file over HTTPS while using no-cache.

七颜 2024-10-27 15:32:06

您可以通过指定 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

You can fix this by specifying your Cache-Control header as follows:

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

This way you can still specify your cache settings and it will work with https.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文