Reputation: 249
I am downloading images in a loop and when I set my loop to for (var i = 0; i < 100; i++) {
I have no problem downloading images. but as soon as I change my i
range to go more than 99 (inclusive of 100) so anything like: for (var i = 100; i < 200; i++) {
OR for (var i = 0; i < 200; i++) {
I get an error. Here is my code:
var fs = require('fs');
var request = require('request');
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
var arrLength = allProducts.length;
for (var i = 0; i < arrLength; i++) {
let imgName = allProducts[i].code;
let imgUrl = allProducts[i].nofrillsImgLink
download(`${imgUrl}`, `./allProductsImg/${imgName}.png`, function(){
});
};
And here is the error I am getting:
events.js:287
throw er; // Unhandled 'error' event
^
Error: Invalid URI "undefined"
at Request.init (.../node_modules/request/request.js:273:31)
at new Request (.../node_modules/request/request.js:127:8)
at request (.../node_modules/request/index.js:53:10)
at Request._callback (.../downloadImg.js:7:5)
at self.callback (.../node_modules/request/request.js:185:22)
at Request.emit (events.js:310:20)
at Request.init (.../node_modules/request/request.js:273:17)
at new Request (.../node_modules/request/request.js:127:8)
at request (.../node_modules/request/index.js:53:10)
at Function.head (.../node_modules/request/index.js:61:12)
Emitted 'error' event on Request instance at:
at Request.init (.../node_modules/request/request.js:273:17)
at new Request (.../node_modules/request/request.js:127:8)
[... lines matching original stack trace ...]
at Function.head (.../node_modules/request/index.js:61:12)
Upvotes: 0
Views: 341
Reputation: 249
Changed the code to this and it worked:
for (var i = 0; i < 11952; i++) {
let imgName = allProducts[i].code;
let imgUrl = allProducts[i].nofrillsImgLink
request({
url : `${imgUrl}`,
//make the returned body a Buffer
encoding : null
}, function(error, response, body) {
//will be true, body is Buffer( http://nodejs.org/api/buffer.html )
console.log(body instanceof Buffer);
//do what you want with body
//like writing the buffer to a file
fs.writeFile(`./allProductsImg/${imgName}.png`, body, {
encoding : null
}, function(err) {
if (err)
throw err;
console.log('It\'s saved!');
});
});
};
Upvotes: 0
Reputation: 83
Can you change this code from
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
to
var download = function(uri, filename, callback){
if(uri !== undefined){
request.head(uri, function(err, res, body){
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
}
};
Upvotes: 1