As of Node.js v6.0.0 using the constructor method has been deprecated and the following method should instead be used to construct a new buffer from a base64 encoded string:
var b64string = /* whatever */;
var buf = Buffer.from(b64string, 'base64'); // Ta-da
For Node.js v5.11.1 and below
Construct a new Buffer and pass 'base64' as the second argument:
var b64string = /* whatever */;
var buf = new Buffer(b64string, 'base64'); // Ta-da
If you want to be clean, you can check whether from exists :
if (typeof Buffer.from === "function") {
// Node 5.10+
buf = Buffer.from(b64string, 'base64'); // Ta-da
} else {
// older Node versions, now deprecated
buf = new Buffer(b64string, 'base64'); // Ta-da
}
Answer from Matt Ball on Stack Overflownode.js - NodeJS: How to decode base64 encoded string back to binary? - Stack Overflow
How can I do Base64 encoding in Node.js? - Stack Overflow
buffer.write(buffer, 'base64')
How can I convert a base64 string to ArrayBuffer?
What is Base64 encoding?
Can Base64 encode images?
How does Base64 work?
Buffers can be used for taking a string or piece of data and doing Base64 encoding of the result. For example:
> console.log(Buffer.from("Hello World").toString('base64'));
SGVsbG8gV29ybGQ=
> console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'))
Hello World
The Buffer constructor is a global object, so no require is needed. Buffers created with strings can take an optional encoding parameter to specify what encoding the string is in. The available toString and Buffer constructor encodings are as follows:
'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set.
'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8.
'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF).
'base64' - Base64 string encoding.
'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node.
The accepted answer previously contained new Buffer(), which is considered a security issue in Node.js versions greater than 6 (although it seems likely for this use case that the input can always be coerced to a string).
The Buffer constructor is deprecated according to the documentation.
The code snippets should read:
console.log(Buffer.from("Hello World").toString('base64'));
console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'));
After this answer was written, it has been updated and now matches this.
function base64ToArrayBuffer(base64) {
var binaryString = atob(base64);
var bytes = new Uint8Array(binaryString.length);
for (var i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
Using TypedArray.from:
Uint8Array.from(atob(base64_string), c => c.charCodeAt(0))
Performance to be compared with the for loop version of Goran.it answer.
For context, I'm trying to store images in blob format in an sqlite database. I send the image in post data in base64 format, then when the request is received in express, I run
Buffer.from(req.body.img, 'base64'); to convert it into binary and store it in the database. However, when I retrieve the data and convert it back to base64, it produces an invalid base64 string like
dataimage/jpgbase64<base64data> when it should be data:image/jpg;base64,<base64data>
I'm using prisma to manage my database.
Is this an issue with the actual encoding/decoding, prisma, or sqlite?
Edit: encoding the base64 into binary, and then immediately encoding the binary back into base64 (without saving to the database) produces the same issue.
new Buffer(...) has been deprecated for a while now, go for Buffer.from(...)
a simple example might be:
var utf8encoded = Buffer.from(base64encoded, 'base64').toString('utf8');
In order to convert from a base64 encoded String to utf8 you can use the following:
var base64encoded = '0KHQvdCw0YfQsNC70LAg0YHQvtC30LTQsNC10YLRgdGPINGA0LXRiNC10YLQutCwINC/0YDQvtGB 0YLRgNCw0L3RgdGC0LLQsCDQstC+0L7QsdGA0LDQttC10L3QuNGPLiZuYnNwOzxkaXY+PGJyPjwv ZGl2PjxkaXY+0JfQsNGC0LXQvCDQvdCwI';
var utf8encoded = (new Buffer(base64encoded, 'base64')).toString('utf8');