简体   繁体   中英

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..');
});

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM