gpt4 book ai didi

javascript - axios response.data.pipe 不是函数

转载 作者:行者123 更新时间:2023-12-03 08:06:32 30 4
gpt4 key购买 nike

我正在使用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

最佳答案

.pipe() 只能在流上使用。要在流中获取 axios 响应,请添加一个带有 responseType:'stream'

的配置对象
    const { data } = await axios.get(url, { responseType: "stream" });
data.pipe(fs.createWriteStream("sample.jpg"));

关于javascript - axios response.data.pipe 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72178121/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com