I am using Express, Node.js, and Mongodb to create the webpage that uploads and displays the image file.
I saved the binary of image in mongodb using schema.
Here is my little bit of code in index.js and db.js..
var Post = mongoose.Schema({
image: {data: Buffer, contentType: String}
});
var post= new Post({ });
post.image.data=fs.readFileSync(req.file.path);
post.image.contentType='image/png';
and here is the part of mongo shell after I submitted image file and searched for post, and its image field
"image: {"data:BinData(0,"iVBORw0KGgoAAAANSUhEUE....(I
just cut off the rest of the code.. cuz it was too long)
rkJggg=="),
"contentType" : "image/png" }
so it seemed like it's successfully saving the binary data of image file in mogngodb,
but my problem is how to display the image on the webpage now using binary data. How do I convert binary buffer data to create image tag??
<img src= "data:{{image.contentType}};base64,{{image.data}}">
I tried this, but it gives me an error:
Failed to load resource: net::ERR_INVALID_URL
Could you guys please let me know how do I solve this?? I will really appreciate for your help :(((