I have an image (png) file stored in my MongoDB as binary data. My MongoDB schema looks like below :
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
ImageSchema = new Schema (
{ title : String,
picture: { content: Buffer, contentType: String }
}
);
The mustache code looks like below:
var template = "{{picture.content}} <h1>{{title}}</h1>";
var rendered = Mustache.render(template, imageObject);
$('#target').html(rendered);
The data in MongoDB looks like below, it has been shortened as it is too long:
"picture" : {
"contentType" : "image/png",
"content" : BinData(0,"iVBORw0KGgoAAAANSUhEUg ...")
}
Below is how I am storing it into MongoDB using multer :
dbmodel.findOne(req.params.title, function(err, ) {
records.picture.content = fs.readFileSync(req.files.path);
records.picture.contentType = 'image/png';
records.save(function (err) {
if(!err) {
res.send("Image is saved into MongoDB");
} else {
res.send(400).send();
}
})
What is the way to convert this binary data to image and especially fit it into the Mustache template for display ? Also please let me know if the image needs to be saved in some other way and how for easier display using Mustache.
Thanks.