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.

Answer from thejh on Stack Overflow
🌐
Base64.Guru
base64.guru › converter › decode › image
Base64 to Image | Base64 Decode | Base64 Converter | Base64
The “Base64 to Image” converter will force the decoding result to be displayed as an image, even if it is a different file type. Therefore, if you are not sure that your Base64 string is an image, use the Base64 to file converter since it is capable of displaying various file types.
Discussions

How can I convert image buffer to string base64?
I am uploading image using Filepond library to MongoDB using Mongoose. I want to show the image in the map's marker using pop-up feature. Image I am getting back in the format { location: { type: ' More on stackoverflow.com
🌐 stackoverflow.com
Encode Image in buffer to Base64
Hi, • Hardware Platform:Jetson ... crop faces and stored in a directory. The cropping of images is successful. I want to convert the faces(images) in buffer to base64 before writing them to the disk. How...... More on forums.developer.nvidia.com
🌐 forums.developer.nvidia.com
1
0
August 12, 2021
mysql - How to convert Buffer to base64 image in Node js - Stack Overflow
For Example: 'data:image/png;base64,iVBORw0KGgoAAAANS' ... Save this answer. ... Show activity on this post. Since you’re receiving a Buffer back as output, Buffer.toString('base64') will convert the raw binary data in the buffer to a base64 representation of the data. More on stackoverflow.com
🌐 stackoverflow.com
Reading from buffer to base64
I'm trying to store images in base64 format in a db. I tried storing the base64 encoded versions of the images and reading them back, but I don't get the full picture back as some parts of it get corrupted for some reason. ... Why do you store images as base64 strings? Why not as blob? For debugging, I would remove the database from the equation and test encoding and decoding step by step. ... Error when decoding base64 with buffer... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @divinehycenth8 › convert-a-base64-data-into-an-image-in-node-js-d82136576e35
Convert a Base64 data into an Image in Node.js | by Divine Hycenth | Medium
November 15, 2020 - You need to first convert your string into Buffer before saving it as a real image otherwise, you’re going to run into issues. ... Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding.
🌐
GitHub
gist.github.com › sid24rane › bdf557cf9f835181a994439da0b5b82a
Node.js Base64 Encode Decode -> Image · GitHub
'use strict'; // node v8.11.3 const Buffer = require('buffer').Buffer; const path = require('path'); const fs = require('fs'); /** * @param {string} filename */ function encode_base64(filename) { fs.readFile(path.join(__dirname, '/public/', filename), function(error, data) { if (error) { throw error; } else { let buf = Buffer.from(data); let base64 = buf.toString('base64'); // console.log('Base64 ' + filename + ': ' + base64); return base64; } }); } /** * @param {string} base64str * @param {string} filename */ function decode_base64(base64str, filename) { let buf = Buffer.from(base64str, 'base64'); fs.writeFile(path.join(__dirname, '/public/', filename), buf, function(error) { if (error) { throw error; } else { console.log('File created from base64 string!'); return true; } }); } encode_base64('ddr.jpg'); decode_base64('any_base64_string_goes_here', 'rane.jpg'); Copy link ·
🌐
DEV Community
dev.to › dnature › convert-a-base64-data-into-an-image-in-node-js-3f88
Convert a Base64 data into an Image in Node.js - DEV Community
December 17, 2021 - You need to first convert your string into Buffer before saving it as a real image otherwise, you're going to run into issues. ... Luckily, Node.js provides a native module called Buffer that can be used to perform Base64 encoding and decoding.
🌐
GitHub
gist.github.com › crspiccin › 790796a68e7178404de4
Node.js convert an image to Base 64 · GitHub
// function to encode file data to base64 encoded string function base64_encode(file) { // read binary data const bitmap = fs.readFileSync(file); // convert binary data to base64 encoded string return bitmap.toString('base64'); } Copy link ·
🌐
Base64Encode.org
base64encode.org › enc › buffer
Base64 Encoding of "buffer" - Online
Encode buffer to Base64 format with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
npm
npmjs.com › package › node-base64-image
node-base64-image - npm
May 10, 2026 - Easily encode images (from URLs or local files) to Base64 strings or Buffer objects, and decode them back into image files.
      » npm install node-base64-image
    
Published: May 10, 2026
Version: 3.0.0
Find elsewhere
🌐
NVIDIA Developer Forums
forums.developer.nvidia.com › accelerated computing › intelligent video analytics › deepstream sdk
Encode Image in buffer to Base64 - DeepStream SDK - NVIDIA Developer Forums
August 12, 2021 - Hi, • Hardware Platform:Jetson ... crop faces and stored in a directory. The cropping of images is successful. I want to convert the faces(images) in buffer to base64 before writing them to the disk. How......
🌐
Medium
medium.com › @koteswar.meesala › convert-array-buffer-to-base64-string-to-display-images-in-angular-7-4c443db242cd
Convert Array buffer to base64 string to display images in Angular 7 | by Kotesh Meesala | Medium
June 9, 2019 - Thanks to Tin.K for pointing out this. const STRING_CHAR = TYPED_ARRAY.reduce((data, byte)=> {return data + String.fromCharCode(byte);}, '')); ... Now, to obtain the base64 string, pass the string of characters to window.btoa() method which ...
🌐
Stack Overflow
stackoverflow.com › questions › 72568169 › reading-from-buffer-to-base64
Reading from buffer to base64
Now, I'm trying to read the image back from the db and decode it into a base64 string. I've tried simply this: const image = await db.get(); // here it's of type buffer return image.toString("base64"); // want to return it as type string in base64
🌐
Cycling '74
cycling74.com › forums › saving-large-base64-images-with-node-js
Saving large base64 images with node.js - Javascript Forum | Cycling '74
March 1, 2023 - const Buffer = require('node:buffer'); const path = require('path'); const Max = require('max-api'); const fs = require('fs'); Max.addHandler("png", (data) => { var buf = Buffer.alloc(262144, 'base64'); data.copy(buf, data); fs.writeFile('image_temp.png', buf, function(err, result) { if(err) post('error', err); }); Max.outlet("done"); }); ... hey John, this n4m code last ran in 2021, but it did write base64 images (coming in from Runway-ML) to disk as png images at 1024x1024.
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-node-js
Encoding and Decoding Base64 Strings in Node.js
November 25, 2024 - To save the data back in its raw PNG format, we simply pass the Buffer object to our fs.writeFileSync method and it does the conversion for us. Base64 encoding is one of the most common ways of converting binary data into plain ASCII text. It is a very useful format for communicating between one or more systems that cannot easily handle binary data, like images in HTML markup or web requests.
🌐
Base64 Image Encoder
base64-image.de
Convert Images to Base64 Online — Free Encoder & Optimizer | base64-image.de
Free online tool to optimize images and convert them to base64 encoding. Drag & drop your files, copy to clipboard, and use the result in HTML and CSS. Supports JPEG, PNG, GIF, WebP, SVG, and 8 more formats.
🌐
Bobby Hadz
bobbyhadz.com › blog › convert-image-to-base64-using-node
Convert an Image or an Image URL to base64 in Node.js | bobbyhadz
Optionally add the data URI prefix, e.g. 'data:image/png;base64,' + base64String ... Copied!import fs from 'fs'; // 👇️ if you use CommonJS require() // const fs = require('fs') function toBase64(filePath) { const img = fs.readFileSync(filePath); return Buffer.from(img).toString('base64'); } const base64String = toBase64('./house.png'); console.log(base64String); const withPrefix = 'data:image/png;base64,' + base64String; console.log(withPrefix);
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › how-base64-encoding-and-decoding-is-done-in-node-js
How Base64 Encoding and Decoding is Done in Node.js ? - GeeksforGeeks
January 8, 2025 - Base64 encoding and decoding in Node.js are straightforward with the Buffer class. This capability is particularly useful for handling binary data that needs to be transmitted over text-based protocols or stored in text formats. Whether you're dealing with strings, images, or other binary files, Node.js makes it easy to encode and decode Base64 data efficiently.
🌐
Google Groups
groups.google.com › g › nodejs › c › n-wpLFfjZH8
Convert Base64 Encoded String Back to an Image and Write to Disk
November 23, 2010 - > > https://gist.github.com/711297 · Sorry, seems I didn't get your original e-mail. The below works for me, result is an image of a keyboard. var data = /* snip */; buffer = new Buffer(data, 'base64'); console.log(data.length, buffer.length); require('fs').writeFileSync('image.jpg', buffer, ...