Internet Explorer 9 不使用文件名作为内联附件 [英] Internet Explorer 9 does not use file name for inline attachments

查看:21
本文介绍了Internet Explorer 9 不使用文件名作为内联附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置内联 PDF 文档的文件名的 Servlet 中使用此代码:

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 属性来为 Save As 准备默认文件名.相反,它使用请求 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/* 的 URL 模式时,如有必要,您可以在 servlet 中动态获取文件名部分,如下所示(例如,定位所需的 PDF 文件和/或在标题中设置正确的 filename 以获得更体面的网络浏览器):

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆