Force S3 PDF to be viewed in browser instead of download

穿精又带淫゛_ 提交于 2020-08-01 16:41:49

问题


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


回答1:


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




回答2:


You have to set your putObject params as follows to view pdf instead of download.

 params = {
        Bucket: process.env.S3_BUCKET,
        Key: <fileName>,
        Body: <fileContent>,
        ContentDisposition:"inline",
        ContentType:"application/pdf"
    };



回答3:


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.




回答4:


You need to provide both these meta data inorder to view the file instead of download:

Content-Disposition: attachment
Content-Type: application/pdf


来源:https://stackoverflow.com/questions/46690640/force-s3-pdf-to-be-viewed-in-browser-instead-of-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!