3

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 ?

6
  • Is myBase64Url just plain base64 data, or is it prepended with the mime type? What issue were you having with atob? Commented Apr 8, 2018 at 18:40
  • atob is not supported in IE. My base64 URL is prepended with pdf mimetype Commented Apr 8, 2018 at 18:54
  • That's the problem-- the mime type will need to be removed before this will work. Commented Apr 8, 2018 at 18:57
  • Removed the mimetype and , tried the same code. Only better thing is its opening a blank pdf file. Couldn't see any data. See updated code in the question. Commented Apr 8, 2018 at 19:06
  • You may need to remove .toString('binary'); as well Commented Apr 8, 2018 at 19:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.