Convert binary data to base64 with javascript
To prevent 'InvalidCharacterError' error, you need to do this:
var base64EncodedStr = btoa(unescape(encodeURIComponent(rawData)));
Use a FileReader to encode your image as a data URL:
jQuery.ajax({...})
.done(function (r) {
var reader = new FileReader(
reader.onload = (function(self) {
return function(e) {
document.getElementById("img").src = e.target.result;
}
})(this);
reader.readAsDataURL(new Blob([r]));
});