Internet Explorer 9 does not use file name for embedded attachments

I use this code in Servlet, which sets the file name of the attached 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 part of the URL path followed by ".pdf" (for "/ some / url / invoice" this is "invoice .pdf" ")

Is this a known bug? Is there a workaround?

+6
source share
1 answer

This is indeed the default behavior of IE. It does not use the filename attribute of the Content-Disposition header in any way to prepare the default file name for Save As. Instead, it uses the last piece of request URL path information.

I recommend rewriting servlets and / or links so that the desired file name is provided as part of the request path information, rather than, for example, the request parameter.

So instead

 <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 matching the URL pattern /pdfservlet/* with the template, you can, if necessary, capture part of the file name dynamically in the servlet as follows (for example, to find the desired PDF file and / or set the correct filename in the header for more decent web browsers):

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

This, by the way, is regardless of whether it served as a built-in or as an attachment.

+11
source

Source: https://habr.com/ru/post/906086/


All Articles