A JSON response has been linked on the user's request to retrieve an excel document. The structure of the response is as follows:
{
"format": // file extn ----only xls
"docTitle": //file name
"document" :// base 64 encoded data
}
The attempt was made to handle the API on the frontend by decoding the 'document' key using the atob() function.
downloadTemplate() {
this.fetchData.fetchDownloadTemplate().subscribe(
res => {
let docString = atob(res.document);
let format = res.format;
let fileName = res.docTitle;
let file = new Blob([docString], {type: "text/xls"});
let url = URL.createObjectURL(file);
let dwldLink = document.getElementById('template_link');
dwldLink.setAttribute("href", url);
dwldLink.setAttribute("download", fileName);
document.body.appendChild(dwldLink);
dwldLink.click();
},
err => {
console.log(err.responseJSON.message);
})
}
However, there seems to be corruption in the data. After some investigation, it was discovered that atob() decodes using the ASCII charset, whereas the encoding was done using the UTF-8 charset.
If you have any suggestions for an alternative method to decode the data with UTF-8 charset in TypeScript, please share. The Angular (v4+) project I am currently working on throws an error when using JS validators.