在IE中,文件下载失败了https

时间:2011-03-04 19:50:11

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

我正在尝试通过HTTPS和&amp ;;下载文件它在IE中失败但与Firefox&铬:

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

请帮忙

4 个答案:

答案 0 :(得分:4)

当用户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";
}

答案 1 :(得分:1)

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-&gt;工具菜单 - &gt;互联网选项 - &gt;高级选项卡 一直到底部的安全部分。 清除“不要将加密的页面保存到磁盘”的检查 关闭所有Internet Explorer窗口 启动IE并再次下载文件

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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