getting corrupted file while converting base64 file to image in angularjs can anyone suggest me how to convert base64 file to image in angularjs
I am using this meth
Needed this in Angular 8, so i modified the answer a bit to typescript and directly to file, since you have the mimetype from the datastring, you might as well use that to create the file.
dataURItoBlob(dataURI : any, fileName : string) : File{
// convert base64/URLEncoded data component to a file
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new File([ia],fileName, {type:mimeString});
}
all credits go to @byteC0de with the answer https://stackoverflow.com/a/35401651/1805974
Only reason I would post the answer here is due to google keeps sending me to this page.