简体   繁体   English

如何在节点 typescript 中将 blob 转换为 base64

[英]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.我正在获取一个 blob 图像以响应对节点 ts 中微服务的内部调用。 I want to convert it to a Base64 so that I can use it to display it in an EJS image tag.我想将它转换为 Base64 以便我可以使用它在 EJS 图像标签中显示它。

I have tried to do it with the Buffer but the base64 it's giving me is incorrect.我试图用缓冲区来做,但它给我的 base64 是不正确的。

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

I have also tried the FileReader but getting an error that FileReader is undefined.我也尝试过 FileReader 但收到 FileReader 未定义的错误。

HERE这里

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM