今天就跟大家聊聊有关使用Node.js怎么批量下载图片,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
首先要获取到图片链接:
const img = [
"https://cache.tdyun.com/upload/information/20200622/114/7732.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7733.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7734.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7735.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7736.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7737.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7738.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7739.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7740.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7741.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7742.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7743.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7744.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7745.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7746.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7747.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7748.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7749.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7750.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7751.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7752.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7753.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7754.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7755.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7756.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7757.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7758.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7759.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7760.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7761.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7762.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7763.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7764.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7765.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7766.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7767.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7768.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7769.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7770.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7771.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7772.jpg",
"https://cache.tdyun.com/upload/information/20200622/114/7774.jpg"
];
批量下载图片:
var fs = require('fs');
var request = require("request");
img.forEach(src => {
var writeStream = fs.createWriteStream(src.split('/2019/08/')[1]);
var readStream = request(src)
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.js怎么批量下载图片有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。