Display images from gridfs with nodejs

238 Views Asked by At

I use a api sails and mongo for storing images, side front nodeJs I want download X images, just one image, the code is functionally :

db.open(function (){
    gfs.exist({ _id: id }, function(err, found) {
        if (err) {
            return err.negotiate(err);
        }

        if (!found) {
            return res.negotiate('No file found');
        }

        gfs.createReadStream({ _id: id }).pipe(res);
    });
});

For download x images......no ideas....

Thank you for your help

1

There are 1 best solutions below

0
On BEST ANSWER
  i am doing it by using this 



   if (fileExists(image_name) == false) { //check the file exist or not   
      var file = fs.createWriteStream( image_name);
                        http.get(url, function (response) {
                                response.pipe(file);
    })else{
    console.log('already exist.');
    }
    });

hope this helps.