I am converting image data held in an arrayBuffer into a string and then base64 encoding it using the following:
var base64EncodedImage = arrayBufferToBase64(response);
arrayBufferToBase64 = function(buffer) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa(binary);
}
When I try to add this data to the src attribute of an image tag
this.imageTag.src = 'data:image/jpeg;base64,' + base64EncodedImage;
..it doesn't display the image, due, I assume to some error in the base64 encoding. When I examine the encodedData, at around character 5000 there is an ellipsis (…) which I assume is what's causing the problems. What is the btoa function doing here and how can I fix it? Is it due to bad data in the arrayBuffer or am I doing something wrong in the binary conversion?