Getting binary data without using arraybuffer

I have the following resource:

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)}; } } } }); 

what he does is get the binary content of the file from the server and return the uri of the data with the underlying data inside.

I have to say that this call cannot be replaced with a simple src tag to a URL, as there are some authentication headers.

this works fine in new browsers, but I want to maintain compatibility with older browsers, so the problem with the array is the problem, is there a way to do all this without arraybuffer?

I tried to remove the response type and convert the string to resp.data, using what is described here but not successful.

+7
javascript angularjs
source share
1 answer

Take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding . I have not tested them, but there are a number of algorithms for converting between byte arrays and base64 URIs. In particular, the base64EncArray function seems to be what you are looking for.

+2
source share

All Articles