Download The Images By Node(request)

753 views Asked by At

I want to down some image on the site ,the image are saved by

http://a.com/1.jpg
http://a.com/2.jpg
http://a.com/3.jpg

So, I write the code to do it

My code is:

var request = require('request');
var fs = require('fs');
var mkdir = require('mkdirp');

var preUrl = 'http://a.com/';
var dir = './images';

mkdir(dir, function(err) {
if(err) {
    console.log(err);
}
});
for(var i = 1; i <= 10; i++){
  var trueUrl = preUrl + i + '.jpg';
  var name = i + '.jpg';
   request(trueUrl, function(err, response, body) {
  if(!err && response.statusCode == 200) {
      download(trueUrl, dir, name);
      console.log("Done" + trueUrl);
                }
      });                
}
var download = function(url, dir, filename) {
request.head(url, function(err, res, body) {
    request(url).pipe(fs.createWriteStream(dir + '/' + filename));
});
};

but there is some error in the code.

the code cant't run the

request(trueUrl, function(err, response, body) {
      if(!err && response.statusCode == 200) {
          download(trueUrl, dir, name);
          console.log("Done" + trueUrl);
                    }
          });

So,What should I do?


Now, I found the console will output the done http://a.com/10.jpg.

and last, the download image is only 10.jpg

3

There are 3 answers

1
Hyo Byun On

Use url.resolve when joining url elements to prevent errors like this. (https://nodejs.org/api/url.html#url_url_resolve_from_to)

url.resolve('/one/two/three', 'four')         // '/one/two/four'
url.resolve('http://example.com/', '/one')    // 'http://example.com/one'
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
2
kaxi1993 On

Hey man I edited your code and now it works i wrote example trueUrl of image you can coment this line don't use for loop while you are using async request

var request = require('request');
var fs = require('fs');
var mkdir = require('mkdirp');

var preUrl = 'http://a.com/';
var dir = './images';

mkdir(dir, function(err) {
if(err) {
    console.log(err);
}
});

var n = 10, i = 0;
function downloadImage() {
  var trueUrl = preUrl + i + '.jpg';
  var name = i + '.jpg';
  trueUrl = "http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/4/11/1397210130748/Spring-Lamb.-Image-shot-2-011.jpg"
  request(trueUrl, function(err, response, body) {
    if(!err && response.statusCode == 200) {
      download(trueUrl, dir, name);
    }
    i++;
    if(i >= 10) {
      return;
    } else {
      downloadImage();
    }
  }); 
  console.log('i=' + i)
}
var download = function(url, dir, filename) {
  request.head(url, function(err, res, body) {
      request(url).pipe(fs.createWriteStream(dir + '/' + filename));
  });
};

downloadImage();
0
rccoder On

I soved the Problem!

use request-promise, no request