在 IE 中通过 https 下载文件失败
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如用户 SquidScareMe 所写,通过 SSL 下载 Office 文件时,您必须忽略/不要触及 Office 文件的缓存设置。
我有一个
.ashx
处理程序,其中有一个片段,例如:使用此函数:
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:With this function:
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
此问题的解决方案是在 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.
您可以通过指定 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