Node.js Javascript don't download images from array

对着背影说爱祢 提交于 2021-02-11 14:57:35

问题


I'm implementing a image downloader because I want display them from client and not from URLs, inside a class I have a method that takes URLs from a datafile and then download them in samefolder. messaggi[] array is a result of reading datafile and I simplify in this snippet declaring it (of course in my code isn't declared as you see but it absolutly works as this result). the problem is that in this snippet doesn't arrive to download images and sometimes just download 1 of them and is unreadable.

var NodeHelper = require("node_helper")
const fsWriter = require("fs");
const http = require('http');
var messaggi = [http://myurl/link1.jpg, http://myurl/link2.png, http://myurl/link3.jpg];
      for (let i=0; i < messaggi.length; i++) {
        var request = http.get(messaggi[i], function(response) {
          console.log("GET" + i)
          var filename = messaggi[i]
          filename = filename.substr(filename.lastIndexOf("/")+1)
          console.log("FILENAME: " + filename)
          let file = fsWriter.createWriteStream(FILEPATH + filename);
          response.pipe(file);
        });
      }

来源:https://stackoverflow.com/questions/60869910/node-js-javascript-dont-download-images-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!