使用Node.js怎么批量下载图片

今天就跟大家聊聊有关使用Node.js怎么批量下载图片,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

成都创新互联主要从事成都网站制作、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务乐昌,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

首先要获取到图片链接:

const img = [
  "/upload/otherpic54/7732.jpg",
  "/upload/otherpic54/7733.jpg",
  "/upload/otherpic54/7734.jpg",
  "/upload/otherpic54/7735.jpg",
  "/upload/otherpic54/7736.jpg",
  "/upload/otherpic54/7737.jpg",
  "/upload/otherpic54/7738.jpg",
  "/upload/otherpic54/7739.jpg",
  "/upload/otherpic54/7740.jpg",
  "/upload/otherpic54/7741.jpg",
  "/upload/otherpic54/7742.jpg",
  "/upload/otherpic54/7743.jpg",
  "/upload/otherpic54/7744.jpg",
  "/upload/otherpic54/7745.jpg",
  "/upload/otherpic54/7746.jpg",
  "/upload/otherpic54/7747.jpg",
  "/upload/otherpic54/7748.jpg",
  "/upload/otherpic54/7749.jpg",
  "/upload/otherpic54/7750.jpg",
  "/upload/otherpic54/7751.jpg",
  "/upload/otherpic54/7752.jpg",
  "/upload/otherpic54/7753.jpg",
  "/upload/otherpic54/7754.jpg",
  "/upload/otherpic54/7755.jpg",
  "/upload/otherpic54/7756.jpg",
  "/upload/otherpic54/7757.jpg",
  "/upload/otherpic54/7758.jpg",
  "/upload/otherpic54/7759.jpg",
  "/upload/otherpic54/7760.jpg",
  "/upload/otherpic54/7761.jpg",
  "/upload/otherpic54/7762.jpg",
  "/upload/otherpic54/7763.jpg",
  "/upload/otherpic54/7764.jpg",
  "/upload/otherpic54/7765.jpg",
  "/upload/otherpic54/7766.jpg",
  "/upload/otherpic54/7767.jpg",
  "/upload/otherpic54/7768.jpg",
  "/upload/otherpic54/7769.jpg",
  "/upload/otherpic54/7770.jpg",
  "/upload/otherpic54/7771.jpg",
  "/upload/otherpic54/7772.jpg",
  "/upload/otherpic54/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怎么批量下载图片有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前标题:使用Node.js怎么批量下载图片
网页网址:http://pwwzsj.com/article/ggdcoo.html