node.js node-red - Converto Base64 string to uint8array

1.1k Views Asked by At

I am using Node Red to implement a web service and I am racking my brains to convert a base64 string to byte array (uint8array) or convert buffer to uint8array.

One "node" of my node-red flow outputs an image as a buffer or a base64 string. I need to pass the responsed image into a web service which requires an uint8array base image.

There is a lot of answers using atob and btoa but node red does not support it.

Below is the output buffer format that I need to convert enter image description here

And this is the buffer format what I want:

enter image description here

Here is the documentation of the web service I want to call:

https://demous-cdb.thereforeonline.com/theservice/v0001/restun/help/operations/CreateDocument

enter image description here

enter image description here

I have tryed a lot of ways:

function toArrayBuffer(myBuf) {
   var myBuffer = new ArrayBuffer(myBuf.length);
   var res = new Uint8Array(myBuffer);
   for (var i = 0; i < myBuf.length; ++i) {
      res[i] = myBuf[i];
   }
   return myBuffer;
}

=====

Also tryed to use...

var buf = Buffer.from(b64string, 'base64')

The solution above does not generates an uint8array

Do you have any idea?

1

There are 1 best solutions below

3
On

The base64 node (https://flows.nodered.org/node/node-red-node-base64) should convert the base64 to buffer which you can then use as input to what ever node you are using to send to the webservice.

The NodeJS Buffer is a subclass of Uint8Array, they are just ways to represent collections of bytes.