I want to get http.get to download a list of image files. I have a for loop to go through the list and fill variable file_savedest with a string that represents the save location. So http.get gets called on each loop iteration, how do I feed the file_savedest into http.get so it know the file to download and save.
var file_savedest = dir+"/"+iname;
http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk
});
res.on('end', function(){
fs.writeFile(file_savedest, imagedata, 'binary', function(err){
if (err){
console.log(err);
} else {
console.log("File:" + file_savedest + " saved");
}
});
});
});
Use a closure:
(function(file_savedest){
http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk
});
res.on('end', function(){
fs.writeFile(file_savedest, imagedata, 'binary', function(err){
if (err){
console.log(err);
} else {
console.log("File:" + file_savedest + " saved");
}
});
});
});
}(dir+"/"+iname));