Store Image file in Binary data in mongoose schema and Display image in html form Store Image file in Binary data in mongoose schema and Display image in html form express express

Store Image file in Binary data in mongoose schema and Display image in html form


First of all, you have to convert buffer data to base64. You can do it in back-end or front-end it does not matter. Just use yourBufferData.toString('base64'). Then you can use it.

However, I would suggest another way to store images instead of storing binary data. Assuming you use nodejs. You can create image in a repository with that binary data using fs.writeFile method. Then you can store that image path in record (db). AFter that, just put the file path into ng-src="file path which you saved". Here is the example which I use:

var path = 'upload/profiles/' +req.body.userId + '_profile.jpg';      fs.writeFile(path, base64data, function(err) {        if (err) return next(err);        User.findByIdAndUpdate({          _id: req.body.userId        }, {          $set: {            profileImg: 'upload/profiles/' +req.body.userId + '_profile.jpg'          }        }, function(err, user) {          if (err) return next(err);          return res.send(user);        });      });  <img ng-src="savedpath">