Dan P.
Dan P.

Reputation: 1775

Downloading a remote image gives error : stream.js:94 Error: ENOENT, open 'path/to/file'

I'm trying to download a remote image and save it in the images folder on my server.

I get the error Error: ENOENT, open 'path/to/file'

I use:

function download (uri, filename, callback) {
  request.head(uri, function(err, res, body){
    var r = request(uri).pipe(fs.createWriteStream(filename));
    r.on('close', callback);
  });
};

var imgURL = "http://www.nasa.gov/images/content/711375main_grail20121205_4x3_946-710.jpg"

var newImgName = "/images/imageName.jpg"
// If I do var newImgName = "imageName.jpg", it works.

download(imgURL, newImgName, function () {
  console.log('Done downloading..');
});

Upvotes: 1

Views: 401

Answers (1)

ZachRabbit
ZachRabbit

Reputation: 5174

Because there is a slash at the beginning of the newImgName, it's trying to put the image in the images folder in the root of your hard drive. If you're trying to put it in the images folder where your code is, try this:

var newImgName = "./images/imageName.jpg";

Upvotes: 2

Related Questions