I get Buffer Data from a storage like <Buffer 48 65 79 20 74 68 65 72 65 21>
for instance. I needed to convert it into a blob. But when i tried doing a toString() to get encoded text just like how a browser would render an attachment , i get all texts as unicode characters. I am in need of a blob I can send to UI along with other params in JSON , which could be used for view using HTML5 FileReader API. Could you please favour.
What i tried was below, where buffer refers to the data as in first line.
let binBuffer = Buffer.from(buffer,'binary').toString('utf8');
In Node. js, the Buffer. toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.
The Buffer. toJSON() method returns the buffer in JSON format. Note: The JSON. Stringify() is the method which can also be used to return the data in JSON format.
Buffers have a toString() method that you can use to convert the buffer to a string. By default, toString() converts the buffer to a string using UTF8 encoding. For example, if you create a buffer from a string using Buffer. from() , the toString() function gives you the original string back.
You cannot directly manipulate the contents of an ArrayBuffer ; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer. A Blob object represents a file-like object of immutable, raw data.
How to convert a Buffer data to JSON in Node.js? Published September 23, 2020 To convert a Buffer to JSON, you can use the toJSON () method in the Buffer instance. For an example, let's say we have an array with some data like this, Now let's convert this data to buffer using the from () method in the Buffer class.
Once the Blob class is available, the conversion itself is pretty simple: If you need to transfer a buffer of data through JSON, you can encode the buffer using a text-based encoding like hexadecimal or base64.
Buffers in Node.js: The Buffer class in Node.js is used to perform operations on raw binary data. Generally, Buffer refers to the particular memory location in memory. Buffer and array have some similarities, but the difference is array can be any type, and it can be resizable. Buffers only deal with binary data, and it can not be resizable.
It's best if you have a separate HTTP request for the binary data. If for some reason this isn't possible, consider a binary format like CBOR rather than JSON. First of all, Node.js didn't have the Blob class until versions v15.7.0 and v14.18.0, so you need to import the Blob class if you haven't already:
Buffer
to a JavaScript Blob
:First of all, Node.js didn't have the Blob
class until versions v15.7.0
and v14.18.0
, so you need to import the Blob
class if you haven't already:
// NOTE: starting from Node.js v18.0.0, the following code is not necessary anymore
// CJS style
const { Blob } = require("buffer");
// ESM style
import { Blob } from "buffer";
NOTE: it appears that in Node.js v14/v15/v16/v17 (last checked: as of
v14.19.2
,v15.14.0
,v16.14.2
andv17.9.0
), theBlob
class is marked as experimental and not stable. Instead, in Node.jsv18.x.x
theBlob
class is marked as stable.
UPDATE 2022-04-25: starting with Node.js version
18.0.0
, you don't have to manually import theBlob
class anymore, but you can use it straight away in your code!
Once the Blob
class is available, the conversion itself is pretty simple:
const buff = Buffer.from([1, 2, 3]); // Node.js Buffer
const blob = new Blob([buff]); // JavaScript Blob
Buffer
via JSON:If you need to transfer a buffer of data through JSON, you can encode the buffer using a text-based encoding like hexadecimal or base64.
For instance:
// get the buffer from somewhere
const buff = fs.readFileSync("./test.bin");
// create a JSON string that contains the data in the property "blob"
const json = JSON.stringify({ blob: buff.toString("base64") });
// on another computer:
// retrieve the JSON string somehow
const json = getJsonString();
const parsed = JSON.parse(json);
// retrieve the original buffer of data
const buff = Buffer.from(parsed.blob, "base64");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With