Internet Explorer 9不使用内联附件的文件名 [英] Internet Explorer 9 does not use file name for inline attachments
问题描述
我在Servlet中使用此代码,该Servlet设置内联PDF文档的文件名:
I use this code in a Servlet which sets the file name of the inlined PDF document:
response.setContentType("application/pdf");
response.setContentLength((int) file.length());
response.setHeader("Content-disposition", "inline; filename=\"" + file.getName() + "\"");
但是这在IE 9中不起作用:另存为...对话框仅显示URL的最后一个路径部分后跟.pdf(对于/ some / url / invoice,它是invoice.pdf)
However this does not work in IE 9: the "Save as..." Dialog shows only the last path part of the URL followed by ".pdf" (for "/some/url/invoice" it is "invoice.pdf")
这是一个已知的错误?是否有解决方法?
Is this a known bug? Is there a workaround?
推荐答案
这确实是IE的默认行为。它不以任何方式使用 Content-Disposition
标头的 filename
属性来为
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.
这篇关于Internet Explorer 9不使用内联附件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!