Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

servlets - Internet Explorer 9 does not use file name for inline attachments

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() + """);

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

So, instead of

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

or

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

you need to use

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...