Use The Base64 Preview Of The Binary Data Response (zip File) In Angularjs
I always get this error in the downloaded zip file C:\Users\me\Downloads\test.zip: Unexpected end of archive My current code is: var blob = new Blob([data], { // data here is the b
Solution 1:
So I just went and ditched using the ajax.service.js
, that we have, for this specific call.
I used the xhr
snippet from this answer. I just added the headers necessary for our call: tokens and auth stuff.
And I used this code snippet for the conversion thing.
And the code looks like this:
fetchBlob(url, function (blob) {
// Array buffer to Base64:var base64 = btoa(String.fromCharCode.apply(null, newUint8Array(blob)));
var blob = newBlob([base64ToArrayBuffer(base64)], {
type: 'octet/stream',
});
var zipUrl = window.URL.createObjectURL(blob);
var fileName = orderNo;
fileName += ' Attachments ';
fileName += moment().format('DD-MMM-YYYY');
fileName += '.zip';
downloadFile(null, fileName, null, zipUrl, null); // create a hidden anchor tag and trigger download
});
functionfetchBlob(uri, callback) {
var xhr = newXMLHttpRequest();
xhr.open('GET', uri, true);
xhr.responseType = 'arraybuffer';
var x = AjaxService.getAuthHeaders();
xhr.setRequestHeader('auth_stuff', x['auth_stuff']);
xhr.setRequestHeader('token_stuff', x['token_stuff']);
xhr.setRequestHeader('Accept', 'application/octet-stream');
xhr.onload = function (e) {
if (this.status == 200) {
var blob = this.response;
if (callback) {
callback(blob);
}
}
};
return xhr.send();
};
functionbase64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = newUint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
};
return bytes;
}
Post a Comment for "Use The Base64 Preview Of The Binary Data Response (zip File) In Angularjs"