How to get image file from Mongo using Sails.JS + GridFS?

I am currently building a website with Sails.js and am stuck fetching an image file from GridFS. I am successfully uploading a file using skipper-gridfs to my mongo gridfs. I don't know how to display the file correctly (I'm new to Sails.js and Node)

Here is my code for getting an image file from gridfs looks like in FileController.js (I am using gridfs stream):

show: function  (req, res, next) {
    var mongo = require('mongodb');
    var Grid = require('gridfs-stream');
    var buffer="";

    // create or use an existing mongodb-native db instance
    var db = new mongo.Db('testDb', new mongo.Server("", 27017), {safe:true});
    var gfs = Grid(db, mongo);

    // streaming from gridfs
    var readstream = gfs.createReadStream({
      filename: 'e1ecfb02-e095-4e2f.png'

    //check if file exist
      filename: 'e1ecfb02-e095-4e2f.png'
    }, function (err, found) {
      if (err) return handleError(err);
      found ? console.log('File exists') : console.log('File does not exist');

    //buffer data
    readstream.on("data", function (chunk) {
        buffer += chunk;
        console.log("adsf", chunk);

    // dump contents to console when complete
    readstream.on("end", function () {
        console.log("contents of file:\n\n", buffer);


When I ran it, the console didn't show anything. There is also no error.

How do I fix this?

Additional question:

  • Better and easier to store / read a file to / from a local disk instead of using gridfs?
  • Am I choosing the correct gridfs stream to fetch the gridfs file form?

source to share

2 answers

In the code skipper-gridfs

and there there is a 'read' method that takes a value fd

and returns the required file corresponding to that value. So you just need to pull this file out of mongo by this method and send it in response. It should work with a file.

download: function (req, res) {
    var blobAdapter = require('skipper-gridfs')({
        uri: 'mongodb://localhost:27017/mydbname.images'

    var fd = req.param('fd'); // value of fd comes here from get request, function(error , file) {
        if(error) {
        } else {
            res.send(new Buffer(file));


Hope this helps :)

Additional questions:

  • Yes, using gridfs is better in both performance and efficiency. And usually mongodb has a 16MB limit, probably for binaries, but using gridfs you can store any size file, it splits them into chunks and saves them.

  • The extraction has been shown above.



You can now use skipper-gridfs in sails to manage your uploads / downloads.

var blobAdapter = require('skipper-gridfs')({uri: 'mongodb://' });



.upload(blobAdapter().receive(), function whenDone(err, uploadedFiles) {
  if (err) return res.negotiate(err);
  else return res.ok({
    files: uploadedFiles,
    textParams: req.params.all()


Download, callback);


Remember that the filename will change after it is loaded into mongo, you must use the filename returned in the first answer.



All Articles