我正在使用axios下载nodejs中的图像。我添加了以下代码
const axios = require("axios").default;
const fs = require("fs");
const url =
"https://images.pexels.com/photos/11431628/pexels-photo-11431628.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260";
async function downloadImage() {
try {
const { data } = await axios.get(url);
data.pipe(fs.createWriteStream("sample.jpg"));
} catch (error) {
console.log(error);
}
}
downloadImage();
但是我得到了以下错误
TypeError: data.pipe is not a function
发布于 2022-05-09 20:57:22
.pipe()
只能在流上使用。要在流中获得axios响应,可以使用responseType:'stream'
添加一个配置对象
const { data } = await axios.get(url, { responseType: "stream" });
data.pipe(fs.createWriteStream("sample.jpg"));
https://stackoverflow.com/questions/72178121
复制相似问题