0

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?

asked Oct 14, 2022 at 16:22

1 Answer 1

1

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);
answered Oct 14, 2022 at 16:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.