使用 ASP.NET 在 IE8 中通过 https 下载文件

发布于 2024-08-10 19:16:20 字数 1335 浏览 2 评论 0原文

我试图让用户可以通过一个重定向按钮从我们的网站下载 Excel 电子表格:

Response.Redirect(string.Format("../excel/ExcelForm.aspx?pathName=&fileNameDisplay={0}&fileNameUnique={1}", "spreadsheet.xls", fileName));

aspx 页面只是通过 Response 对象发回文件,如下所示:

 Response.ContentType = "application/vnd.ms-excel";
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay);
 Response.WriteFile(Server.MapPath(pathName + fileNameUnique));
 Response.Flush();
 Response.End();

一切正常我的机器上,但是当我们将其放在服务器上时,https 与无缓存设置相结合会给我们一个错误,提示“Internet Explorer 无法下载 [blahblahblah]”。显示 Excel 按钮的页面上的缓存设置:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("cache-control", "private, no-cache, must-revalidate no-store pre-check=0 post-check=0 max-stale=0");
HttpContext.Current.Response.Cache.SetNoServerCaching();

当我删除这些行时,一切正常。但是,我不能因为其他原因删除它们。因此,我尝试将以下行添加到 ExcelForm.aspx 中,然后将内容添加到标题中:

Response.ClearHeaders();

这只是给我“Internet Explorer 无法从 [url] 下载 ExcelForm.aspx”。这就是我被困住的地方。建议?

I'm trying to make it possible for the user to download an Excel spreadsheet from our site, by having a button that redirect through this:

Response.Redirect(string.Format("../excel/ExcelForm.aspx?pathName=&fileNameDisplay={0}&fileNameUnique={1}", "spreadsheet.xls", fileName));

The aspx page just sends back the file through the Response object, like this:

 Response.ContentType = "application/vnd.ms-excel";
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay);
 Response.WriteFile(Server.MapPath(pathName + fileNameUnique));
 Response.Flush();
 Response.End();

Everything works just fine on my machine, but when we're putting it on the server, the https in combination with no-cache settings gives us an error saying "Internet Explorer cannot download [blahblahblah]". The cache settings on the page displaying the excel button:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("cache-control", "private, no-cache, must-revalidate no-store pre-check=0 post-check=0 max-stale=0");
HttpContext.Current.Response.Cache.SetNoServerCaching();

When I remove those lines, everything works just fine. However, I'm not allowed to remove them for other reasons. So I tried adding the following line to the ExcelForm.aspx just before it adds stuff to the header:

Response.ClearHeaders();

Which just gives me "Internet Explorer cannot download ExcelForm.aspx from [url]". And that's where I'm stuck. Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来分手还会想你 2024-08-17 19:16:20

我最近在从 MVC 控制器方法导出 CSV 文件时遇到了类似的问题。我发现添加:

      Response.ClearHeaders();
      Response.Clear();

解决了我在 IE 中的问题

希望这有帮助!

I had a similar problem myself recently when exporting CSV files from an MVC controller method. I found that adding:

      Response.ClearHeaders();
      Response.Clear();

Solved the problem for me in IE

Hope this helps!

万劫不复 2024-08-17 19:16:20

我也遇到了同样的问题,

当我用谷歌搜索时,我发现响应头中的“no chache”设置即以下代码是问题的原因。

Response.AppendHeader("Pragma", "no-cache") 
Response.AppendHeader("Cache-Control", "no-cache") 
Response.AppendHeader("max-age", "0") 

一些博客说,要解决这个问题,您应该在Web服务器和所有客户端计算机上的Windows注册表中进行一些修改(:O),但在每台客户端计算机上进行注册表设置是不可行的。

根本原因是响应头中没有缓存设置,所以我只是

Response.ClearHeaders() 

在将要下载的内容添加到响应头之前添加。代码如下所示,

Response.ClearHeaders() 
Response.ContentType = "application/ms-excel" 
Response.AppendHeader("content-disposition", "attachment; filename=""" + fileName + """") 
Response.BinaryWrite(fileBytes) 
Response.End() 

已经解决了这个问题。

享受!!!

I also faced the same issue,

When I googled it, I found that "no chache" settings in response header i.e. following code is the reason for the issue.

Response.AppendHeader("Pragma", "no-cache") 
Response.AppendHeader("Cache-Control", "no-cache") 
Response.AppendHeader("max-age", "0") 

Some of the blogs says, to fix this issue, you should do some modification in Windows registry on Webserver and on all client machines(:O), well it is not feasible to do registry settings on every client machine.

The root cause is no-cache settings in response header, so I just added

Response.ClearHeaders() 

before adding content to be downloaded to the response header. The code is as shown below,

Response.ClearHeaders() 
Response.ContentType = "application/ms-excel" 
Response.AppendHeader("content-disposition", "attachment; filename=""" + fileName + """") 
Response.BinaryWrite(fileBytes) 
Response.End() 

It has fixed the issue.

Enjoy!!!

花桑 2024-08-17 19:16:20

我遇到了完全相同的问题,我无法通过 IE8 下载二进制流

根据此页面上的信息,我的新代码看起来像

  • Response.ClearHeaders();
  • Response.ContentType =“应用程序/八位字节流”;
  • Response.AppendHeader("content-disposition", string.Format("附件; filename={0}", "nameofthefile.exe"));
  • Response.BinaryWrite(字节);
  • 响应.End();

现在它在所有浏览器下都可以正常工作

I had the exact same issue I was unable to download a binary stream trough IE8

According to the information on this page, my new code looks like

  • Response.ClearHeaders();
  • Response.ContentType ="application/octet-stream";
  • Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}", "nameofthefile.exe"));
  • Response.BinaryWrite(bytes);
  • Response.End();

and now it works like a charm under all browsers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文