I have an ArrayBuffer(of an excel file) in my javascript app, which I need to offer as a downloadable excel file on a button click. I generated the Arraybuffer using the excel4node library. I tried the following :
let element = document.createElement('a');
let excelBuffer = await fetchService.getBuffer();
let excelString = String.fromCharCode(...excelBuffer.data);
console.log(excelString);
element.setAttribute('href', 'application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelString));
element.setAttribute('download', 'dataFile.xlsx');
element.style.display = 'none';
shadowRoot.appendChild(element);
element.click();
shadowRoot.removeChild(element);
This code is executed on a button click. But this doesn’t seem to work. I either get Malformed URI errors, or the browser tries to open a new url which reads “application/blocked”. Logging the converted excelString using String.fromCharCode logs some weird symbols. I got to know this method uses UTF-16 encoding for the conversion, but the ArrayBuffer seems to be utf-8 encoded. I also tried this which didnt work :
var blob = new Blob([excelBuffer.data], {type: "application/vnd.ms-excel"});
element.setAttribute('href', 'application/vnd.ms-excel;charset=utf-8,' + blob);
element.setAttribute('download','dataFile.xlsx');
How do I make this work?