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

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

我正在尝试通过 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();
    }

请帮忙

最佳答案

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

关于c# - 文件下载在 IE 中通过 https 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5198530/

相关文章:

c - 如何使用 openssl API 读取 DER 格式的服务器证书?

c# - EF 6.0 代码优先 : How to Synchronize an existing database with existing domain classes

html - 使用 "left"作为移动的 CSS 关键帧动画在 Internet Explorer 中不起作用

jquery - jQuery .load 方法和 Internet Explorer 中换行符的问题

sslsplit : Received privsep req type 02

ios - 将敏感数据附加到 iOS 应用程序中使用的 https url 的末尾是否安全?

c# - 无法在 C# 的通用约束中使用结构类型

c# - 如果具有 runat ="server"属性,如何使 div 可拖动

c# - GemBox 从电子表格或 Flexcel 中检索计算值

internet-explorer - Socket.io:为什么 htmlfile 优于 XHR-Polling?