ENCRYPT DECRYPT IMAGE IN BASE 64 USING JAVA SCRIPT




These days usage of mobile api  generally we get image encoding in base 64 and we store in our data base directly.

While fetching some kind of issue facing like some images will be crashed and not loaded.Here is the solution for us.



1.) FIRSTLY HOW IS TO BE UPLOAD IMAGE IN TO SERVER 
AND ALSO STORED AND FETCHING IN BASE64 FORMAT IN MONGO DB

Try following example using server side
fs.readFile(req.files.fileName[0].path, function(err, data) {
   var base64data = new Buffer(data).toString('base64');
 //console.log(base64data)
 
 response.mongoDB.insertData(collection,{"image" : base64data},{},
      function(result)
        {    
          response.requestPage.renderPage(res,constant.fileUsageView,constant.rok, {"images" : result})
          
        });
 
 
  })


2)  I am using JADE template so this code looks like this 
Try following example using Try it option available at the top right corner of the below sample code box −
h2 Images
   each image, i in images
    img(src="data:image/jpeg;base64,#{image.image}" alt="#{image._id}")




JUST TRY IT VERY EASY TO USE ;)


0 Comments