简体   繁体   English

显示存储在 S3 存储桶中的 PDF 文件

[英]Display PDF file stored in S3 bucket

Im using multer-s3 to upload a PDF to S3 bucket, and it works, but now I need to display the PDF without downloading it (just like this ) and I already tryied to use iframe, embed and object, each one with the src='link_to_s3_file' and when I access the page it just download the file.我使用 multer-s3 将 8825587763888 上传到 S3 存储桶,它可以工作,但现在我需要在不下载它的情况下显示 PDF(就像这样),我已经尝试使用 iframe,嵌入和 object,每个都带有 src ='link_to_s3_file' 当我访问该页面时,它只是下载文件。 That's my code to configure the upload:那是我配置上传的代码:

const multer = require('multer')
const multerS3 = require('multer-s3')
const AWS = require('aws-sdk')

AWS.config.update({
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_REGION
})
const s3 = new AWS.S3()

const upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: process.env.AWS_S3_BUCKET,
        acl: 'private',
        contentDisposition: 'attachment',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname });
        },
        key: function (req, file, cb) {
            cb(null, file.originalname)
        }
    })
})

How can I display the PDF instead of download it?如何显示 PDF 而不是下载它?

In order to open PDF directly, without downloading it, you should change 2 things:为了直接打开 PDF 而无需下载,您应该更改两处:

ContentDisposition='inline'
ContentType='application/pdf'

If you're sure that multerS3.AUTO_CONTENT_TYPE will recognize it correctly, you might not have to change this, but ContentDisposition is for sure one thing you have to change.如果您确定multerS3.AUTO_CONTENT_TYPE会正确识别它,您可能不必更改它,但 ContentDisposition 肯定是您必须更改的一件事。 attachment means that it will download it and inline is to open it in browser directly. attachment表示会下载, inline是直接在浏览器中打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM