-1

I have file coming in request.file object. If I console.log(request.file) It prints

{
  fieldname: 'file',
  originalname: 'Screenshot from 2021-06-23 18-34-25.png',
  encoding: '7bit',
  mimetype: 'image/png',
  destination: 'public/assets',
  filename: 'file-1628356843810.png',
  path: 'public/assets/file-1628356843810.png',
  size: 620962
}

Now I want to convert file to base64 string and I did

  const encoded = request.file?.buffer.toString("base64")
  console.log(encoded)

It says

TypeError: Cannot read property 'toString' of undefined
Prasanga Thapaliya
  • 657
  • 1
  • 8
  • 19
  • Does this answer your question? [How can I check for an empty/undefined/null string in JavaScript?](https://stackoverflow.com/questions/154059/how-can-i-check-for-an-empty-undefined-null-string-in-javascript) – Hazel へいぜる Aug 09 '21 at 17:35

1 Answers1

-3
const fs = require('fs');
// read binary data of the file
const binaryData = fs.readFileSync(request.file);
// convert binary data to base64 string
const base64String = new Buffer(binaryData).toString('base64');
Tyler2P
  • 2,324
  • 26
  • 22
  • 31