hakuna
hakuna

Reputation: 6701

Angular: Convert base64 string to Byte Array in IE

I am trying to convert a base64 string to byte array and open it as a pdf file in IE. The only problem is atob is not supported in IE, so trying to use Buffer like this:

let b64Data = myBase64Url.split(',', 2)[1];
var byteArray = new Buffer(b64Data ,'base64').toString('binary');
var blob = new Blob([byteArray], {type: 'application/pdf'});
window.navigator.msSaveOrOpenBlob(blob); 

I am getting a popup successfully to open the file

enter image description here

But the file is corrupted

enter image description here

What am i doing wrong ? Is there a better way to convert base64 to byte array in IE ?

Upvotes: 3

Views: 9327

Answers (1)

user184994
user184994

Reputation: 18281

In order for the base64 to be properly decoded, it must be only the base64 data, i.e. no mimetype information preceding it.

You will also need to remove .toString('binary') so that you're passing a buffer instead of a string.

Upvotes: 1

Related Questions