PDF created on HTTP request won't open in Inte

2020-07-17 07:30发布

问题:

I've got servlet that makes a PDF on the fly to the response, based on some parameters on the request. When I try to open/download this file in IE I get the following message:

"Unable to download [filename] from [site]. Unable to open this Internet site. The requested site is either unavaliable or cannot be found. Please try again later."

This is what i set to the response:

response.setHeader("Content-Disposition", "attachment;filename=" + title + ".pdf");

response.setContentType("application/pdf");

Everything is working fine with other browsers.

Update:

I've tried inline instead of attachment. This will open a new window instead of a open/save dialog (I would prefer to have the dialog), the error message is gone, but the new window is just empty in IE. Still working with other browsers.

I've tried simple, double and no quotes around the filename, which does not contain any unescaped characters. Double and no quotes makes no difference, but single ones are appended to the filename, which I of course don't want.

The filename contains no unescaped characters.

I've checked out the tests on this page, which says that attachement, with filename, no quotes, is passed for all browsers. This is what was used from the start.

Adding Content-Lenght header makes no difference.

Flying Saucer is used to make the PDF, by the way.

回答1:

I suggest taking a look at http://greenbytes.de/tech/tc2231/ which lists a whole bunch of test cases for how different browsers behave with different values for the Content-Disposition header. My guess is that the title contains spaces or some other special characters that need to be escaped, and IE is currently barfing on the non-escaped value.



回答2:

Have you try the following headers:

response.setHeader("Content-Length", lenght);
response.setHeader("Content-Disposition", "inline; filename='" + filename + "'");

Filename should be excaped.



回答3:

This been a while, but thought I should update this, as a solution was found.

The CMS in use was adding some headers wich I did not take into consideration at first:

  • Cache-Control: private
  • Pragma: no-cache

Seems to be a download problem with IE, if there is no caching. Those headers were changed in an upgrade of the CMS, and that solved the problem.

Example:

  • Cache-Control: public, max-age=31536000
  • Date: Thu, 06 Dec 2012 14:14:14 GMT
  • Expires: Fri, 06 Dec 2013 14:14:14 GMT


回答4:

I had the same issue, ended up being that IE will not handle response code 201 (all other browsers handle it). When I changed it to 200 IE downloaded the file just fine.

This post pointed me in the right direction.



回答5:

By below code i can the pdf in the IE browser. response.setHeader("Content-Disposition", "attachment;filename=" + title + ".pdf");

Thanks a lot.