使用 ASP.NET 在 IE8 中通过 https 下载文件
我试图让用户可以通过一个重定向按钮从我们的网站下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近在从 MVC 控制器方法导出 CSV 文件时遇到了类似的问题。我发现添加:
解决了我在 IE 中的问题
希望这有帮助!
I had a similar problem myself recently when exporting CSV files from an MVC controller method. I found that adding:
Solved the problem for me in IE
Hope this helps!
我也遇到了同样的问题,
当我用谷歌搜索时,我发现响应头中的“no chache”设置即以下代码是问题的原因。
一些博客说,要解决这个问题,您应该在Web服务器和所有客户端计算机上的Windows注册表中进行一些修改(:O),但在每台客户端计算机上进行注册表设置是不可行的。
根本原因是响应头中没有缓存设置,所以我只是
在将要下载的内容添加到响应头之前添加。代码如下所示,
已经解决了这个问题。
享受!!!
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.
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
before adding content to be downloaded to the response header. The code is as shown below,
It has fixed the issue.
Enjoy!!!
我遇到了完全相同的问题,我无法通过 IE8 下载二进制流
根据此页面上的信息,我的新代码看起来像
现在它在所有浏览器下都可以正常工作
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
and now it works like a charm under all browsers