2011-03-04 130 views
0

我试图下载通过HTTPS &它无法在IE浏览器,但火狐& Chrome浏览器的完美文件:文件下载失败通过HTTPS在IE

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

请帮

+0

您是否收到错误或请求是否消失?任何有关您正在经历的其他信息都会有所帮助。 – 2011-03-04 19:53:10

+0

你是什么意思,它失败了?你会得到什么样的错误? – 2011-03-04 19:53:28

+0

它如何失败? – 2011-03-04 19:53:59

回答

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

我发现如果你像这样''Response.AddHeader(“Cache-Control”,“no-store,no-cache”)手动设置头部,'它会工作并使用你的缓存头部。请参阅:http://blogs.msdn.com/b/ieinternals/archive/2009/10/03/internet-explorer-cannot-download-over-https-when-no-cache.aspx。 – 2012-08-30 16:56:21

1

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

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

SOLUTION: 互联网Explorer的>工具菜单 - > Internet选项 - >高级选项卡 转至安全部分的底部一路。 清除了“不将加密的页面到磁盘” 关闭所有Internet Explorer窗口 启动IE检查并下载文件再次

+2

在我看来,这不是一个解决方案,因为用户必须做一些客户端调整。更好的办法是不要通过SSL缓存Office文件。 – 2011-03-04 20:11:25

1

它的解决方案针对此问题是在ISA激活压缩。经过这一步后,网站可以传输文件没有任何问题! 当您尝试在使用无缓存时通过HTTPS传输文件时,会出现此问题。