Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force S3 PDF to be viewed in browser instead of download

So you can force a download by using Content-Disposition: attachment

Content-Disposition: inline is the default and should display in the browser, and it does in fact work with most files like PNG, JPG, etc.

But for some reason somehow when generating a presigned URL from S3, PDF files will always force download even if I don't use the content-disposition: attachment header.

I want to be able to make the PDF open in the browser when the browser allows it

I am using the presigned URL generate call from S3 client http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLJavaSDK.html

like image 613
dominicbri7 Avatar asked Oct 11 '17 14:10

dominicbri7


2 Answers

Check you file's metadata and remove Content-Disposition entry from that file. and set content type according to the file type.

Like for text file Content-Type='text/plain'

image png Content-Type='image/png'

pdf Content-Type=application/pdf

pdfxml Content-Type=application/vnd.adobe.pdfxml

If your file's Content-Type is binary/octet-stream then it will download instead of display.

Thanks

like image 152
Kush Vyas Avatar answered Oct 24 '22 20:10

Kush Vyas


You also need to set the Content-Type correctly. The browser will check the content-type value, and if it isn't something it knows how to display it will always just download the file.

like image 23
Mark B Avatar answered Oct 24 '22 22:10

Mark B