且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Internet Explorer 9不使用内联附件的文件名

更新时间:2022-02-24 09:00:04

这确实是IE的默认行为。它不以任何方式使用 Content-Disposition 标头的 filename 属性来为另存为。相反,它使用请求URL路径信息的最后部分。

That's indeed default behaviour of IE. It does not use the filename attribute of the Content-Disposition header in any way to prepare a default filename for the Save As. Instead, it uses the last part of the request URL path info.

我建议重写您的Servlet和/或链接,以便提供所需的文件名作为部分请求路径信息而不是例如请求参数。

I recommend rewriting your Servlet and/or the links in such way that the desired filename is supplied as part of request path info instead of as for example a request parameter.

因此,而不是

<a href="/pdfservlet">View PDF</a>

<a href="/pdfservlet?file=foo.pdf">View PDF</a>

您需要使用

<a href="/pdfservlet/foo.pdf">View PDF</a>

映射到 / pdfservlet / * $ c的网址格式时$ c>,如有必要,你可以在servlet中动态获取文件名部分,如下所示(例如,找到所需的PDF文件和/或设置正确的文件名 in更好的webbrowsers的标题):

When mapped on an URL pattern of /pdfservlet/*, you can if necessary grab the filename part dynamically in the servlet as follows (for example, to locate the desired PDF file and/or to set the right filename in the header for the more decent webbrowsers):

String filename = request.getPathInfo().substring(1); // foo.pdf

无论是内联还是附件,这都是顺便说一句。

This is by the way regardless of whether it's served inline or as an attachment.