我正在以PDF格式生成HTML网页,然后在本地导出。如何将此文件保存到节点服务器并上传到S3
请找到附带的伪代码
const convertDataToPdf = (exportFlag,cb)=>{ //set to switch between export and save
const doc = new jsPDF();
//... adding metadata and styling the pdf
if(exportFlag) {
doc.save('sample.pdf') //export PDF locally
} else {
cb(doc.output()) //this converts the PDF to raw to send to server
}
}
基于this answer,我将原始PDF数据附加到新的FormData对象,然后调用ajax将原始数据发布到我的nodejs服务器
convertDataToPdf(false, pdfData => {
let formData = new FormData();
formData.append(`file-1`, pdfData)
$.ajax({
url: '/file-upload',
data: formData,
processData: false,
contentType: false,
type: 'POST',
}).then(data => {
console.log('PDF upload to s3 successful!', data)
}).catch(err => {
console.log('Error! PDF Upload to S3 failed', err)
})
});
});
现在,我如何解析服务器上的原始PDF数据并将其上传?
作为替代方案,是否可以在本地保存我的文件,然后将文件上传到s3?
答案 0 :(得分:0)
第一个问题 - 您可以在节点服务器multer
https://www.npmjs.com/package/multer上使用。这样您就不必解码pdf。您只需处理请求并将文件传递给S3(通过S3节点API)。您可以使用mimetype
确保有人向您发送pdf。
当然,如果你有Nginx等应用服务器,你可以限制传输文件。
例如在Nginx client_max_body_size 10M;
中。检查服务器限制更安全,因为顽皮的用户总是可以欺骗您的网络验证。如果您想从后端返回特定的例外,Multer
也会进行尺寸验证。