Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert base64 string to image in angular 4

I have a requirement where I am cropping the image using ng2-image cropper. But the output is of base64, I want to convert it to image source as I have to send it to some server. I have searched but didn't found anything compatible with angular 4 .

like image 731
Ayushi Tomar Avatar asked Nov 28 '17 11:11

Ayushi Tomar


2 Answers

it can be done by using conversion to Blob

dataURItoBlob(dataURI) {
    var binary = atob(dataURI.split(',')[1]);
    var array = [];
  for (var i = 0; i < binary.length; i++) {
     array.push(binary.charCodeAt(i));
  }
 return new Blob([new Uint8Array(array)], {
    type: 'image/jpg'
});
}

    enter code here

 var myFile:Blob=this.dataURItoBlob(myDataUri);
like image 153
Ayushi Tomar Avatar answered Oct 29 '22 18:10

Ayushi Tomar


Please try this another simple way to convert base64 to blob

   fetch(base64)
    .then(res => {
      return res.blob();
    })
    .then(blob => {
      console.log(blob);
    });
like image 22
Gopala raja naika Avatar answered Oct 29 '22 17:10

Gopala raja naika