I have a specific resource that I am utilizing:
function _arrayBufferToBase64(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);
}
var API = $resource(server + 'album', {}, {
get: {
url: server + 'album/:albumId/photo/:photoId',
method: 'GET',
responseType: 'arraybuffer',
headers: {
'AuthToken': 'the secret',
'Accept': 'image/*'
},
interceptor: {
response: function(resp) {
return 'data:'+ resp.headers('Content-Type') + ';base64,' + _arrayBufferToBase64(resp.data)};
}
}
}
});
The purpose of this code is to retrieve the binary content of a file from a server and generate a data URI with the base64 data included.
It's worth noting that a simple src tag cannot be used in place of this call since it requires sending authentication headers along with the request.
Although this functionality works smoothly in modern browsers, compatibility with older browsers poses a challenge due to the arraybuffer usage. Hence, my question is: Is there a way to achieve all this without relying on arraybuffers?
I attempted to remove the response type and convert the string in resp.data by following the instructions mentioned here, but unfortunately, it was not successful.