Display images from GridFs (MongoDB) Display images from GridFs (MongoDB) mongoose mongoose

Display images from GridFs (MongoDB)


Unless you really need to embed images into page, use pipes as of saintedlama's answer.

dHJvb3Blci5qcGVn is base64 encoded string "trooper.jpeg". Please ensure you correctly saved binary data to GridFS at the first instance.

You can query it directly and check content of the file stored in the db.


var Grid = require("gridfs-stream");Grid.mongo = mongo; router.get("/:filename", function(req, res){         gfs = Grid(db);        var readstream = gfs.createReadStream({filename: req.params.filename});         readstream.on("error", function(err){            res.send("No image found with that title");         });        readstream.pipe(res);    });

This is the perfect solution to get images from gridfs


You can just pipe the GridFS read stream to the response without buffering:

app.get('/picture', function(req, res) {  // Set correct content type first  res.contentType('image/png');  fs    .createReadStream('/tmp/Disneygoofy2012.jpeg')    .pipe(res);});

I guess the problem of your buffering solution was that you only pass the first byte package you receive from GridFS to res.send.