Skip to content
Advertisement

How to convert Buffer to base64 image in Node js

I am getting data from my SQL database like this:

How can I convert result to a base64 image in Node.js? For Example: 'data:image/png;base64,iVBORw0KGgoAAAANS'

Advertisement

Answer

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.

Buffer.toString() can also take other encodings, you can read more about the other supported encodings in the docs.

So for your above code you would use this to get your image as base64

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement