Node.JS - Encoding images in base64 using Buffer Node.JS - Encoding images in base64 using Buffer json json

Node.JS - Encoding images in base64 using Buffer


fs.readFile(attachment, function(err, data) {   var base64data = new Buffer(data).toString('base64');   [your API call here]});

It takes some time until the results are there, so by the time you've got the data, the outer scopes execution is already over.


Just specify "base64" as the encoding. Per the docs:

If no encoding is specified, then the raw buffer is returned.

fs.readFile(attachment, {encoding: 'base64'}, function(err, base64data) {   [your API call here]});