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
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";
}
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
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.
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.
来源:https://stackoverflow.com/questions/5198530/file-download-fails-over-https-in-ie