2

I need to uplaod a pdf file from UI(written in Javascript) to Amazon S3 but I am trying to upload the file to the S3, I am getting some unicode format text and when I copy that text to notepad, or say, any other text editor I can the human readable text

I am using pdfmake to get the content of the file and upload it using getBufffer method.

var content = generatePDF(base64Img);
pdfMake.createPdf(content).getBuffer(function (data) {//Code}

The code that i used to upload the file to S3.

 var params = {
        Bucket: bucketName,
        Key: file_name,
        Body: data.toString(),
        ContentType: 'application/pdf'
      }

      s3.upload(params, function (err, data) {
        if (err) {
        // code 
        }else{
         //code
}

The file is getting uploaded successfully but I am getting the text like

!
 " #$%&!' ()*')+,
!
!
!
!

But I am pasting it to other text editor, I am getting

Date: 04/20/19
2
  • 1
    Just remove ToString() form data & then try. Commented Apr 23, 2019 at 5:47
  • When I remove the toString(), I get the below error:TypeError: a._set is not a function Commented Apr 23, 2019 at 6:09

2 Answers 2

4

I solved the above problem by passing the data from getBuffer to S3. In S3, I passed to a buffer like

var data = new Buffer(event.data, 'binary');

uploaded the data to S3.

var params = {
        Bucket: bucketName,
        Key: file_name,
        Body: data,
        ContentType: 'application/pdf'
      }

      s3.upload(params, function (err, data) {
        if (err) {
        // code 
        }else{
         //code
}
Sign up to request clarification or add additional context in comments.

Comments

2

To upload a file from client end directly to s3 bucket you can use multer-s3.

FROM CLIENT END:

axios.post(url, data, {
        onUploadProgress: ProgressEvent => {
          this.setState({
            loaded: (ProgressEvent.loaded / ProgressEvent.total * 100),
          })
        },
      })
        .then(res => { // then print response status
          toast.success('Upload Success!')
        })
        .catch(err => { // then print response status
          toast.error('Upload Failed!')
        })

SERVER SIDE:

    const upload = multer({
    storage: multerS3({
    s3: s3,
    acl: 'public-read',
    bucket: BUCKET_NAME,
    key: function (req, file, cb) {
      UPLOADED_FILE_NAME = Date.now() + '-' + file.originalname;
      cb(null, UPLOADED_FILE_NAME);
      }
     })
    }).array('file');

    app.post('/upload', function (req, res) {
      upload(req, res, function (err) {

        if (err instanceof multer.MulterError) {
          return res.status(500).json(err)
          // A Multer error occurred when uploading.
         } else if (err) {
           return res.status(500).json(err)
          // An unknown error occurred when uploading.
       }
      console.log('REQUEST FILE IS', UPLOADED_FILE_NAME)
      return res.status(200).send(UPLOADED_FILE_NAME)

      // Everything went fine.
      })
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.