无法将 PDF 加载到 S3
Trouble loading PDFs to S3
我正在尝试将本地文件上传到 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]);
});
原来我需要更多地过滤数据:
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");
});
我正在尝试将本地文件上传到 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]);
});
原来我需要更多地过滤数据:
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");
});