Skip to content

Instantly share code, notes, and snippets.

@remotevision
Last active December 23, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remotevision/6708240 to your computer and use it in GitHub Desktop.
Save remotevision/6708240 to your computer and use it in GitHub Desktop.
Base64 image in GridFS
////////////////////////////
// Doesn't work in browser
// expecting image to be displayed (no html)
////////////////////////////
// generic images route
server.get(version+'/images/:id', function(req, res) {
gridfstore.read( req.params.id, function(error,data) {
var img = new Buffer(data.buffer, 'base64');
res.writeHead(200, {
'Content-Length': img.length,
'Content-Type': 'image/jpeg'
});
res.write(img);
res.end();
});
});
////////////////////////////
// works in browser
// image is displayed using html <img src="...">
////////////////////////////
// generic images route
server.get(version+'/images/:id', function(req, res) {
gridfstore.read( req.params.id, function(error,data) {
var img = new Buffer(data.buffer, 'base64');
res.end("<html><body><img src=\"" + img + "\"/></body></html>");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment