原理: 获取二进制流写入文件
import axios from 'axios';
import fs from 'fs';
export async function downloadImage(url: string, filename: string) {try {const response = await axios.get(url, { responseType: 'stream' });response.data.pipe(fs.createWriteStream(filename));} catch (error) {console.error(`Error downloading image: ${error}`);}
}// 使用demo
await downloadImage('https://imgblog.csdnimg.cn/1b3dc99685b74fb4aacd28fba5bc407f.png', `./xxx/logo/${fileName}`)