文件下载结果显示“IE 无法打开此网站”
我对这个很茫然。我已经查遍了,似乎有很多解决方案,但它们对我不起作用。我有一个 CGI::Application 应用程序,可以使用 Spreadsheet::WriteExcel 生成 MS Excel 电子表格。这种方法在相当长的一段时间内运行良好,直到几周前我们的实时服务器出现硬件故障。我们以这次中断为借口升级到 Windows Server 2008(从 2003 年开始)和 Apache 2.2.17(从 2.2.11 开始)。现在,我收到了来自客户的零星(但太频繁而不容忽视)的投诉,他们在尝试下载电子表格时收到此错误:
Internet Explorer 无法从 [站点] 下载 [url]。
Internet Explorer 无法打开此 Internet 站点。请求的站点不可用或无法找到。请稍后重试。
我在 XP、Vista 和 7 上尝试过 IE 7-8,但无法在本地重现此错误。遇到问题的用户每次都会遇到这个问题,而不是随机的。所有投诉均来自 IE 用户,主要针对 IE8。
在阅读了几篇有关错误消息的帖子后,我添加了 -expires
标头,但没有效果。 (由于无法直接测试这一点,我不得不实施修复并等待一天左右,看看人们是否停止抱怨._.
)
sub export_spreadsheet {
my $self = shift;
binmode STDOUT;
my $str;
open my $fh, '>', \$str;
my $workbook = Spreadsheet::WriteExcel->new($fh);
# words words words
$workbook->close;
close $fh;
$self->header_add(-type => 'application/vnd.ms-excel',
-expires => '+1d',
-attachment => 'export.xls');
return $str;
}
请求的标头看起来很正常。请注意,这些是在我的本地计算机上收集的。
HTTP/1.1 200 OK
Date: Tue, 31 May 2011 22:23:17 GMT
Server: Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o mod_perl/2.0.4-dev Perl/v5.10.1
Expires: Wed, 01 Jun 2011 22:23:18 GMT
Content-Disposition: attachment; filename="export.xls"
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Content-Type: application/vnd.ms-excel
Content-Length: 18944
Accept-Ranges: none
Proxy-Connection: Keep-Alive
目前,我们为遇到此问题的客户(无法或不愿意切换到备用浏览器)提供的解决方法是通过自行键入 https
来切换到 SSL。对于那些尝试过并回复我们的人来说,SSL 下载效果很好。推测:是否是下游代理弄乱了我们的标头?这可能就是它在 SSL 中工作而在纯 HTTP 中出错的原因吗? (在这种情况下,服务器升级将是一个不幸的巧合。)
I'm at a loss for this one. I've looked all over and there seem to be a lot of solutions, but they aren't working for me. I've got a CGI::Application app generating a MS Excel spreadsheet with Spreadsheet::WriteExcel. This worked fine for quite some time until our live server had a hardware failure a couple weeks ago. We used the outage as an excuse to upgrade to Windows Server 2008 (from 2003) and Apache 2.2.17 (from 2.2.11). Now, I'm getting sporadic (but too frequent to ignore) complaints from customers receiving this error when trying to download spreadsheets:
Internet Explorer cannot download [url] from [site].
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.
I have tried IE 7-8 on XP, Vista, and 7 and have been unable to reproduce this error locally. The users that have the problem have it every time, not randomly. All complaints have come from IE users, mostly on IE8.
After reading a couple posts about the error message, I added the -expires
header to no avail. (With no way to test this directly, I've had to implement a fix and wait a day or so to see if people stop complaining ._.
)
sub export_spreadsheet {
my $self = shift;
binmode STDOUT;
my $str;
open my $fh, '>', \$str;
my $workbook = Spreadsheet::WriteExcel->new($fh);
# words words words
$workbook->close;
close $fh;
$self->header_add(-type => 'application/vnd.ms-excel',
-expires => '+1d',
-attachment => 'export.xls');
return $str;
}
The headers for the request look normal. These were collected on my local machine, mind you.
HTTP/1.1 200 OK
Date: Tue, 31 May 2011 22:23:17 GMT
Server: Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o mod_perl/2.0.4-dev Perl/v5.10.1
Expires: Wed, 01 Jun 2011 22:23:18 GMT
Content-Disposition: attachment; filename="export.xls"
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Content-Type: application/vnd.ms-excel
Content-Length: 18944
Accept-Ranges: none
Proxy-Connection: Keep-Alive
The current workaround we give to customers (unable or unwilling to switch to an alternate browser) with the issue is to switch to SSL by putting typing the https
themselves. The SSL download works fine for the ones who have tried it and gotten back to us. Speculation: Could it be a downstream proxy messing with our headers? Could that be why it works in SSL and errors in plain HTTP? (The server upgrade would be an unfortunate coincidence in this case.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 http://support.microsoft.com/kb/316431 IE 无法处理在某些情况下,文件未缓存,但随后由某些外部进程打开。这不是完全相同的情况,但正如 EricLaw 在评论中提到的,这可能与
Vary
标题以及下载没有文件名这一事实有关。我会删除该标头并给它一个文件名,IE 应该能够将文件保存到磁盘,以便 Excel 可以打开它。
According to http://support.microsoft.com/kb/316431 IE can't deal with some situations where a file isn't cached but it's then opened by some external process. It's not the exact same case, but as EricLaw mentioned in a comment, it might have something to do with the
Vary
heading and the fact that the download doesn't have a file name.I'd remove that header and give it a filename and IE should be able to save the file to disk so that it can be opened by Excel.
如果整个系统正常工作并且只有下载偶尔失败,那么您也可以尝试为文件名指定一个动态名称。
IF the system as a whole is working and that only the downloads are sporadically failing, then you could also try giving the file name a dynamic name.
我们最近有一个类似的案例,在检查了 整个 一堆 的 无用 答案 在 MS 网站上,我发现了一个有趣的 博客文章进一步阐明了这个问题,主要是关于防止缓存的标头(包括最终解决OP问题的
Vary
标头,+1)。然而,IE 在许多其他情况下也会抛出这个误导性的异常,所以我想我应该在这里添加它,以防它对遇到同样问题的其他人有用。在我们的例子中,事实证明,生成 (Excel) 文件并将其发送到响应的 JSP 作者忘记了确保响应中的文件内容前面不应该有空格。
对于 Java/JSP 文件(我相信您可以将相同的原则应用于其他语言),当您有一些看似无辜的东西时,就会出现问题:
即,将回车符作为 JSP 指令的一部分,而不是 在生成文件内容并将它们发送到响应之前,在它们之间,因为这些行之间的回车符是空白,它设法在 IE 的精致机制中抛出一个虚拟扳手(普通浏览器似乎可以很好地处理这个问题) 。如果您像这样格式化代码:
那么您应该没问题。我确信大多数 Web 开发人员都遇到过类似的问题,但就我而言,已经有一段时间了,我不得不检查 JSP 几次,然后才注意到有一行没有这样做......
We had a similar case recently, and after checking a whole bunch of useless answers on the MS site, I came across an interesting blog post that shed some more light on the issue, mainly about headers that prevent caching (including the
Vary
header that ended up solving the OP's problem, +1).However, IE throws this misleading exception in a number of other cases as well, so I thought I'd add this here in case it's useful to someone else encountering the same problem. In our case, it turned out that the author of the JSP that generated the (Excel) file and sent it to the response had forgotten to make sure that no whitespace should preceed the file contents in the response.
In the case of Java/JSP files (I'm sure you can adapt the same principle to other languages), problems occur when you have something innocent-looking like:
i.e. have the carriage returns as part of the JSP directives instead of between them before you generate the file contents and send them to the response, because the carriage return between such lines is whitespace that manages to throw a virtual spanner in IE's delicate machinery (normal browsers seem to handle this just fine). If you instead format your code like this:
then you should be ok. I'm sure most web devs have come across similar issues, but in my case it had been a while and I had to look over the JSP a few times before noticing that one line wasn't doing that....