Problem loading PDF from S3 in Chrome

I'm having trouble downloading PDF files from Amazon S3 using Chrome.

When I click the link, my controller redirects the request to the file url to S3.

It works fine with Firefox, but nothing happens with Chrome.

However, if I right-click β†’ Save Location, how will the file be uploaded ...

And even a copy of the S3 URL in Chrome will lead to a blank screen ...

Here is some information returned by curl:

Date: Wed, 01 Feb 2012 15:34:09 GMT Last-Modified: Wed, 01 Feb 2012 04:45:24 GMT Accept-Ranges: bytes Content-Type: application/x-pdf Content-Length: 50024 Server: AmazonS3 

My guesses are related to the problem with the content type ... but everything I tried does not work.

+6
source share
2 answers

The canonical type of Internet multimedia for a PDF document is actually application/pdf , as defined in Application Type / PDF Media (RFC 3778) - note that application/x-pdf , while commonly seen and displayed as the media type in Portable Document Format , in particular, does not have the official Media Application Types , which is specified Internet Access Rights (IANA) .

I don’t know why and when application/x-pdf came to life, but obviously Chrome PDF Plugin does not open application documents / x -pdf today.

Therefore, you should be able to initiate other behavior in Chrome by changing the media type of the stored objects accordingly.

Alternative (for authenticated requests)

Another approach would be to force the download of the PDF file , instead of letting Chrome try to open it, which can be done by running Content-Diposition: attachment with your GET request - see the S3 documentation for the GET Object on how to achieve this using the parameter request response-content-disposition , in particular response-content-disposition=attachment , as shown in the section "Example request with parameters that change the value of the response header."

This is only available for authenticated requests, see Request Parameters:

Note

You must sign the request using the authorization header or pre-signed URL using these parameters. They cannot be used with an unsigned (anonymous) request.

+7
source

There is an html solution for this. Since chrome has been updated with HTML5, we can use the new new download attribute!

<a href="http://www.domain.com/painful.pdf">Broken</a>
<a href="http://www.domain.com/painful.pdf" download="notsopainful">Works</a>

+7
source

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


All Articles