在 Internet Explorer 中直接打开下载时文件名出现问题

Issue with file name when directly opening download in Internet Explorer

我们有一个 Java 网络应用程序,它生成报告并直接将它们作为二进制响应提供。直到最近,我们才以这种方式生成 PDF 文件(可直接由浏览器打开),但最近我们还添加了将报告生成为 XLS 文件的功能。在“您想要做什么”弹出框中选择“打开”时,这似乎会在 Internet Explorer 中造成问题。

为报告提供服务的 URL 的格式为“http://localhost:8080/ias/ReportsClientInterface?req=fetch&jobid=2352837_1609341332985”。我们设置的 MIME 类型是“application/vnd.ms-excel”,Content-Disposition header 是“filename=[name].xls”,其中 [name] 可以根据报告的标题。在 Internet Explorer(11 运行 on Windows 10)中,浏览器显示“你想用 [文件名] 做什么?” popup. The [filename] on the popup correctly shows the file name specified by the Content-Disposition header. If "save" is selected, the name chosen for the file is matching that header as well. However, when "open" is selected, Excel shows ReportsClientInterface (which is in the URL as shown above) as the name, instead of the expected report name. Worse, if we leave Excel open and generate a second report, Excel will refuse 打开它,因为第二个报表也尝试使用名称“ReportsClientInterface”打开。

有没有办法强制 IE/Excel 遵守 header 中指定的文件名,从而不会出现此问题?

提前致谢。

Is there a way to force IE/Excel to honor the file name specified in the headers so this issue does not occur?

重新表述您的问题:“有没有办法远程修复此 IE11 错误”?

答案显而易见但令人失望:当然,没有,没有。

通常的解决方法是扰乱服务器的路由系统。您不希望 URL 是 http://localhost:8080/ias/ReportsClientInterface?req=fetch&jobid=2352837_1609341332985,您希望它是 http://localhost:8080/ias/report/descriptiveNameOfReport/[name].xls?jobid=2352837_1609341332985

请注意,作为更笼统的注释,在您的 URL 中使用 ReportsClientInterface 是非常丑陋的,并且表明通常您在编写 Web 服务的路由部分搞砸了。 URL很重要。多注意它们,不要接受任何导致这种混乱的路由系统(路由 = 运行以将 URL,例如 /ias/ReportsClientInterface 绑定到某些处理程序的进程 - java 你写的代码是为了响应这个。那是调用 'routing').

我假设您使用的是自动路由系统,即您可能有一个名为 ReportsClientInterface 的 class 并且现有的路由系统使用它来自动加载 classes。或者你有一些系统会产生例如XML 将此 URL 链接到您的 class。

无论哪种方式,查看您拥有的 Web 框架并找出如何控制路由,然后花点时间清点您公开的每个服务和页面,并想出一个不错的 URL所有这些的计划。然后实现这个。

每个 webframework 的路由都不同,因此在不知道您使用的是哪一个的情况下,我无法进一步详细说明如何完成这项工作。