文档
安装
npm i download
定义方法
Promise download(url, destination?, options?)
url: string 文件地址
destination: string 文件保存目录,如果设置了该参数,就会自动保存
options: Object 其他选项
extract: boolean 解压文件
filename: string 保存文件名
使用示例
const download = require('download');
const fs = require('fs');
let url =
'https://s.cn.bing.net/th?id=OHR.RomanView_ZH-CN6469652448_1920x1080.jpg';
(async () => {
const res = await download(url);
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist');
}
fs.writeFileSync('dist/foo.jpg', res);
})();
(async () => {
await download(url, 'dist');
})();
(async () => {
await download(url, 'dist', {
filename: 'foo.jpg',
});
})();
|