Node.js最简单的下载文件方式
发布时间: 2023-12-07 18:18:44
作者: 王乐园 浏览次数:
1169
新建一个 dw.js文件
代码如下:
var fs = require('fs');
var request = require("request");
for (var i = 0; i < 500; i++) {
let image = `http://www.xxxx.com/${i + 1}.png`
var writeStream = fs.createWriteStream('./imgs/' + image.split('/').pop());
var readStream = request(image)
readStream.pipe(writeStream);
readStream.on('end', function () {
console.log('文件下载成功');
});
readStream.on('error', function () {
console.log(1);
// console.log("错误信息:" + err)
})
writeStream.on("finish", function () {
console.log("文件写入成功");
writeStream.end();
});
}
最后执行 命令: node dw.js
上一篇:(转载)追忆过往青春
下一篇:(转载)时光悠悠 岁月无言