如何将流中的流转换为typecript中的nodejs

发布于 2025-01-21 06:14:46 字数 598 浏览 3 评论 0原文

我正在使用Axis下载如下:

private async downloadImage(imageUrl: string): Promise<Stream> {
  const response = await axios.get<Stream>(imageUrl, {
    headers: {
      'Content-Type': 'image/jpeg',
    },
    responseType: 'stream',
  });
  return response.data;
}

如何将“流”对象转换为“ base64 String”,我从Nodejs中的此静止API中获得?因为我需要附加此图像,以将邮件(由NodeMailer)发送给用户。例如,

attachments: [
  {
    filename: 'image.jpg',
    path: 'data:image/png;base64,[base64Image]',
    cid: 'image',
  },
],

如何在打字稿中为Nodejs实现它?

I'm using axis to download the image as following:

private async downloadImage(imageUrl: string): Promise<Stream> {
  const response = await axios.get<Stream>(imageUrl, {
    headers: {
      'Content-Type': 'image/jpeg',
    },
    responseType: 'stream',
  });
  return response.data;
}

How can I convert the "Stream" object to "base64 string" I get from this RESTFul API in NodeJS? Because I need to attach this image to send the mail (by nodemailer) to the user. Example,

attachments: [
  {
    filename: 'image.jpg',
    path: 'data:image/png;base64,[base64Image]',
    cid: 'image',
  },
],

How can I achieve it for NodeJS in TypeScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味拾荒者 2025-01-28 06:14:46

最后,我发现通过通过流进行转换的方法:

static async streamToBase64(stream: Stream): Promise<string> {
  return new Promise((resolve, reject) => {
    const cbConcat = (base64) => {
      resolve(base64);
    };

  stream
    .pipe(new Base64Encode())
    .pipe(concat(cbConcat))
    .on('error', (error) => {
      reject(error);
    });
  });
}

但是,可以使用一次流...有人知道如何克隆它再次消耗它吗?

Finally, I find the method to convert it by pass through the Stream:

static async streamToBase64(stream: Stream): Promise<string> {
  return new Promise((resolve, reject) => {
    const cbConcat = (base64) => {
      resolve(base64);
    };

  stream
    .pipe(new Base64Encode())
    .pipe(concat(cbConcat))
    .on('error', (error) => {
      reject(error);
    });
  });
}

However, the Stream could be used once... Anyone know how to clone it to consume it again?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文