Arpit Trivedi
Arpit Trivedi

Reputation: 119

How can I convert an blob to base64 in node typescript

I am getting a blob image in response to an internal call to a MicroService in node ts. I want to convert it to a Base64 so that I can use it to display it in an EJS image tag.

I have tried to do it with the Buffer but the base64 it's giving me is incorrect.

response = Buffer.from(response,'binary').toString('base64');

I have also tried the FileReader but getting an error that FileReader is undefined.

Upvotes: 0

Views: 3015

Answers (1)

Scoopex
Scoopex

Reputation: 459

HERE

const b64 = await fetch(url)
      .then((response) => response.buffer())
      .then((buffer) => {
        const b64 = buffer.toString('base64');
        return b64;
      })
      .catch(console.error);

Upvotes: 1

Related Questions