Requirement: I have REST WebService which is used to download a file from the Oracle UCM Server. The file extension is .xlsx
Now, when I invoke the REST WebService passing value of the valid parameter(s), it returns me the response in which the Document Content is Base64Encoded.
Thus, in this post, I will show you the JavaScript function that will Decode and Download the Document Content which is Base64Encoded.
Solution: For the solution of the above requirement we will use the below JavaScript code.
define([], function() {
'use strict';
var PageModule = function PageModule() {};
PageModule.prototype.downloadFile_XLSX = function(fileBytes) {
var blob;
blob = converBase64toBlob(fileBytes,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
var filename = "test.xlsx";
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement("a");
if (link.download !== undefined) {
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
};
function converBase64toBlob(content, contentType) {
contentType = contentType || '';
var sliceSize = 512;
var byteCharacters = window.atob(
content);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset +=
sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {
type: contentType
});
return blob;
}
return PageModule;
});Hence, the solution to our requirement.
If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.
Thanks & Regards,
Susanto Paul
567 total views, 1 views today