Ayushi Tomar
Ayushi Tomar

Reputation: 245

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 .

Upvotes: 7

Views: 12145

Answers (2)

Gopala Raja Naika
Gopala Raja Naika

Reputation: 2669

Please try this another simple way to convert base64 to blob

   fetch(base64)
    .then(res => {
      return res.blob();
    })
    .then(blob => {
      console.log(blob);
    });

Upvotes: 1

Ayushi Tomar
Ayushi Tomar

Reputation: 245

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);

Upvotes: 8

Related Questions