C# IE中通过https下载文件失败
我正在尝试通过HTTPS下载文件&它在IE中失败,但与Firefox和Chrome完美配合: aspx代码如下:C# IE中通过https下载文件失败,c#,internet-explorer,ssl,https,file-transfer,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:C
<%@ 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();
}
请帮助
更新:啊哈!
解决方案:
Internet Explorer->工具菜单->Internet选项->高级选项卡
一直到底部的安全部分。
清除“不将加密页保存到磁盘”上的复选框
关闭所有Internet Explorer窗口
启动IE并再次下载文件
更新:啊哈!
解决方案:
Internet Explorer->工具菜单->Internet选项->高级选项卡
一直到底部的安全部分。
清除“不将加密页保存到磁盘”上的复选框
关闭所有Internet Explorer窗口
启动IE并再次下载文件当用户写入时,通过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";
}
当用户写入时,在通过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";
}
这个问题的解决方案是在ISA上激活压缩。在此步骤之后,网站可以毫无问题地传输文件!
当您尝试在不使用缓存的情况下通过HTTPS传输文件时,会出现此问题 这个问题的解决办法是在ISA上激活压缩。在此步骤之后,网站可以毫无问题地传输文件!
当您尝试在不使用缓存的情况下通过HTTPS传输文件时,会出现此问题 您可以通过如下指定缓存控制头来解决此问题:
<%@ 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();
}
Response.AddHeader(“缓存控制”,“无存储,无缓存”)代码>
这样,您仍然可以指定缓存设置,并且可以使用https
请参阅:您可以通过如下指定缓存控制标头来解决此问题:
<%@ 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();
}
Response.AddHeader(“缓存控制”,“无存储,无缓存”)代码>
这样,您仍然可以指定缓存设置,并且可以使用https
请参阅:您是收到了错误,还是请求消失了?关于你所经历的任何其他信息都会有所帮助。你说失败是什么意思?您遇到的错误类型是什么?错误-->Internet Explorer无法打开此Internet站点。请求的站点不可用或找不到。请稍后再试。您是收到错误,还是请求消失了?关于你所经历的任何其他信息都会有所帮助。你说失败是什么意思?您遇到的错误类型是什么?错误-->Internet Explorer无法打开此Internet站点。请求的站点不可用或找不到。请稍后再试。在我看来,这不是一个解决方案,因为用户必须进行一些客户端调整。更好的方法是不要通过SSL缓存Office文件。在我看来,这不是一个解决方案,因为用户必须进行一些客户端调整。更好的方法是不要通过SSL缓存Office文件代码>它将工作并使用缓存头。请参阅:。我发现,如果您像下面这样手动设置头Response.AddHeader(“缓存控制”,“无存储,无缓存”)代码>它将工作并使用缓存头。请参阅:。