问题
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