简体   繁体   English

无法将 PDF 加载到 S3

[英]Trouble loading PDFs to S3

I am trying to upload local files to S3.我正在尝试将本地文件上传到 S3。 This code works fine for images, but the PDFs are not properly formed.此代码适用于图像,但 PDF 格式不正确。

NodeJS code:节点代码:

const s3=new AWS.S3();
    let type=imgData.split(';')[0].split('/')[1];
    if (type == "pdf")  type="application/pdf";
    else                type="image."+type;
    imgData=new Buffer.from(imgData.replace(/^data:image\/\w+;base64,/, ""), 'base64');
    let params= { Bucket:bucketName, Key:fileName, Body:imgData, ACL:"public-read", ContentType:type };
    s3.upload(params, (err, data)=>{
        if (err) throw err; 
        console.log("Image loaded");
        });

Browser upload:浏览器上传:

<input type="file" id="co-imageUpload" style="display:none">

$("#co-imageUpload").on("change",(e)=>{                                                         
    let myReader=new FileReader();          
    myReader.onloadend=(e)=>{   
        // Send to Node JS
        }                       
    myReader.readAsDataURL(e.target.files[0]);  
    });

Turns out I needed to filter the data more:原来我需要更多地过滤数据:

const s3=new AWS.S3();
let type=imgData.split(';')[0].split('/')[1];
if (type == "pdf")  {
    type="application/pdf";
    imgData=new Buffer.from(imgData.replace(/^data:application\/\w+;base64,/, ""), 'base64');
    }
else{
    type="image."+type;
    imgData=new Buffer.from(imgData.replace(/^data:image\/\w+;base64,/, ""), 'base64');
    }
let params= { Bucket:bucketName, Key:fileName, Body:imgData, ACL:"public-read", ContentType:type };
s3.upload(params, (err, data)=>{
    if (err) throw err; 
    console.log("Image loaded");
    });

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

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