【问题标题】:Trouble loading PDFs to S3无法将 PDF 加载到 S3
【发布时间】:2021-06-10 18:42:15
【问题描述】:

我正在尝试将本地文件上传到 S3。此代码适用于图像,但 PDF 格式不正确。

NodeJS 代码:

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");
        });

浏览器上传:

<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]);  
    });

【问题讨论】:

    标签: javascript node.js pdf amazon-s3 aws-sdk


    【解决方案1】:

    原来我需要更多地过滤数据:

    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");
        });
    

    【讨论】:

      猜你喜欢
      • 2022-08-08
      • 2015-07-02
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多