1

I have to send static images to another Node app

To do this I need to get Base64 from file

This is the function I used in another project (VueJS web app):

export async function getBase64FromURL (path, filename) {
  const fr = new FileReader()
  fr.readAdDataURL(new File(path, filename))

  return await new Promise((resolve, reject) => {
    fr.onloadend = () => {
      resolve(fr.result)
    }
  })
}

NodeJS lacks some functions, for example FileReader() and I found this npm package

But I haven't found a solution for new File(), what can I do?

1

1 Answer 1

0
// Import "fs" lib.
const fs = require('fs');

// Read file as buffer and convert to BASE64.
const fileBase64 = fs.readFileSync('./file.txt').toString('base64');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.