1

Actually I am trying to preview a pdf in chrome and download it. I am writing the byte array in the servlet response object and setting the following headers.

response.setContentType("application/pdf; charset=utf-8"); 
response.setHeader("Content-Disposition", "inline; filename="+fileName+".PDF");

and in jsp action target="_blank".

when I click download button. PDF file is open in new tab. But when I tries to download then it is showing "network error" in chrome and other browsers except firefox.

The actual file format is like 12345.PDF Is there any contentType issue occurs bw .PDF and .pdf

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
Dhivakar
  • 2,081
  • 5
  • 15
  • 18
  • '`ServletResponse` output stream' has nothing to do with the target filename. It is entirely determined by your browser in response to the `Content-disposition` header. – user207421 Aug 12 '20 at 08:15
  • As an aside: combining a `charset` parameter with `application/pdf` does not make any sense at all, it merely invites software to damage the PDF by attempting to apply the charset somehow. – mkl Aug 12 '20 at 12:31

1 Answers1

1

Normally I use Content-Disposition: attachment; filename="foo.bar"; Try adding "attachment; filename="+fileName+".pdf"

attachment indicates resource is downloadable

Also there is a file extensions registry of known types: more info here mozilla doc [How to determine the correct MIME type for your content section] so pdf is registered while PDF isn't.

As a general fallback you could also try "application/octet-stream" writing the file as a binary stream.

Andrew
  • 2,598
  • 16
  • 27
  • Is there any content type issue that occurs between .pdf and .PDF? The actual file format is like 12345.PDF. response.setContentType ("application/pdf"); I am able to download .pdf but not .PDF. – Dhivakar Aug 12 '20 at 07:52
  • "attachment" is directly downloading the file. But here I am preview it then download. – Dhivakar Aug 12 '20 at 07:53
  • Yes there is a file type extensions registry of known types which is why pdf works and PDF doesn't more info here: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Configuring_server_MIME_types – Andrew Aug 12 '20 at 07:59
  • yes I have checked that there is no such MIME type like PDF. Is there any solution to resolve it. – Dhivakar Aug 12 '20 at 09:55
  • Have you tried using "attachment; filename="+fileName+".pdf" in your response header instead? I changed this in my answer. – Andrew Aug 12 '20 at 09:59
  • Actually I am getting the file from FTP Client. So there is file format is in .PDF. In chrome browser it can be preview but its downloading in blank format(means there is no format just a File), if we save with extention .pdf then it is saving but not taking taking the default format. But same file I can save as pdf from firefox browser by default. – Dhivakar Aug 12 '20 at 10:00
  • Yes I tried but same issue. response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename=cndn.pdf"); and in FTP client.connect(ftpUrl); client.login(userName, password); client.setFileType(FTP.BINARY_FILE_TYPE); client.retrieveFile("/"+fileName, os); – Dhivakar Aug 12 '20 at 10:01
  • instead of "inline" I think you need "attachment" to tell the browser to download it. – Andrew Aug 12 '20 at 10:11
  • as a fallback you could try using "application/octet-stream" writing the file as a binary stream but I am not sure if this will work. – Andrew Aug 12 '20 at 10:14
  • ya its working. I have used this "application/octet-stream" but instead of preview its directly downloading as pdf. I should preview first then if we click save then it should download – Dhivakar Aug 12 '20 at 11:11
  • Good I added that the to answer; unfortunately that is mutally exclusive with previewing as it tells the browser its download only. I can only suggest having two workflows in your app one for previewing and one for download. That PDF must be an odd format, maybe poke around in chrome tools network tab to examine the headers when you are able to preview it. – Andrew Aug 12 '20 at 11:19
  • ya ok sure I will explore it. But for .pdf files I can make the preview and I can download it. – Dhivakar Aug 12 '20 at 12:27