简体   繁体   中英

Export a file to Node server and then upload to S3

I'm generating HTML webpage as PDF, and then exporting it locally. How can I save this file to my node server and upload to S3

Please find the attached psuedo code

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
  }
}

Based on a this answer , I'm appending the raw PDF data to a new FormData object, and then an ajax call to post the raw data to my nodejs server

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

Now, how can I parse the raw PDF data on the server and upload it?

As an alternative, is it possible to save my file locally and then upload the file to s3?

  1. First question - you can use on Node server multer https://www.npmjs.com/package/multer . This way you don't have to decode pdf. You just handle request and pass file to S3 (via S3 node API). You can use mimetype to be sure someone is sending you pdf.

  2. For sure if you've got application server such as Nginx, you can limit transfer file. For example in Nginx client_max_body_size 10M; . It's more secure to check limit on server, because naughty users can always cheat your web validations. Multer also has size validation if you would like to return specific exception from your backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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