Search code examples
javascriptnode.jsbase64buffer

Running Buffer.from on base64 string returning Uint8Array instead of Buffer


I need to get an image in the form of Buffer like in the example below: enter image description here

But when I run Buffer.from on a base64 string I'm getting Uint8Array like in the example below: enter image description here

const originalUrl =
      'https://i.picsum.photos/id/621/200/300.jpg?hmac=GgxwZqdPsVQwlM2QhfHoLU8gZ7uo_PP6oD4KmIq-ino';
const response = await axios.get(originalUrl, { responseType: 'arraybuffer' });

const base64Str = response.data.base64Slice();

// returns Uint8Array
const brfFromBase64String = Buffer.from(base64Str, 'base64');

How can I turn a base64 string into a Buffer?


Solution

  • Turn the the Uint8Array,strBuffer, into an ArrayBuffer with .buffer. Then pass it to Buffer.from to get Buffer:

    const buffer = Buffer.from(strBuffer.buffer);