TypeError:“data”参数必须是字符串类型或者 Buffer、TypedArray 或 DataView 的实例。收到 IncomingMessage 的实例

问题描述 投票:0回答:1

我在 cli 脚本中有这个 Nodejs 代码。我正在尝试从一系列链接下载一些图像,但总是收到此错误:

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of IncomingMessage

这是代码:

    imagesLinks.forEach( (link) => {
      let filename = path.basename(link);
      let outputDir = path.format({dir: __dirname, base: `/dadini/${filename}`});
      https.get(link, (res) => {
        fs.writeFileSync(outputDir, res);
      });
    });  

如何正确从链接获取图像并将其传递给

writeFileSync
函数?

javascript node.js arrays
1个回答
0
投票

假设您的 IncomingMessage 是 this,它扩展了 Readable,因此它是一个可读流。您可以将图像数据通过管道传输到可写流。比如:

res.pipe(fs.createWriteStream(outputDir))
© www.soinside.com 2019 - 2024. All rights reserved.