DOM Exception 5 INVALID CHARACTER error on valid base64 image string in javascript

I was just banging my head against the wall on this one for awhile.

There are a couple of possible causes to the problem. 1) Utf-8 problems. There's a good write up + a solution for that here.

In my case, I also had to make sure all the whitespace was out of the string before passing it to atob. e.g.

function decodeFromBase64(input) {
  input = input.replace(/\s/g, '');
  return atob(input);
}

What was really frustrating was that the base64 parsed correctly using the base64 library in python, but not in JS.


I had to remove the data:audio/wav;base64, in front of the b64, as this was given as part of the b64.

var data = b64Data.substring(b64Data.indexOf(',')+1);

var processed = atob(data);