So, I'm having this problem that whenever I'm downloading data(json) as .xlsx file its getting corrupted and unusable, this is the case when this app is deployed on heroku using express server. But when I run a local server on my machine and download the file it works just fine and data is there the way I want.
I'm using XLSX version 0.11.3 and on client side only.
Here's the code:
downloadResData(data) {
let ws = XLSX.utils.json_to_sheet(data);
let wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "data");
let wbout = XLSX.write(wb, {bookType: 'xlsx', type: 'binary'});
function s2ab(s) {
let buf = new ArrayBuffer(s.length);
let view = new Uint8Array(buf);
for (let i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
FileSaver.saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), "data.xlsx");
}
}
So to be clear, the browser is actually generating the final file, and that works with data fed from a local server but not from an express server on Heroku? If that is the case, it's possible you aren't seeing the same data. Try hard-coding an input and testing the function in both places (for example, call downloadResData([{foo:1}, {bar:2}]))
Yes the browser is generating the final .xlsx file. The data(json) source is same(django server) on both the local(running browserify) and heroku server(running express).
Now, these things...
...
downloadResData() {
let ws = XLSX.utils.json_to_sheet([{foo:1}, {bar:2}]);
...
and the file actually opened with the hardcoded data in it.
downloadResData() function is valid but still the downloaded file is corrupt.downloadResData(data) {
console.log(typeof(data));
console.log(data);
let ws = XLSX.utils.json_to_sheet(data);
...

Last check: can you console.log(JSON.stringify(data)) in the failing case, save the resulting string, then hardcode the data to be JSON.parse(str)? (checking if the issue can be reproduced locally with the same failing dataset).
So, I changed the code to:
downloadResData(data) {
console.log(JSON.stringify(data));
let str = JSON.stringify(data);
let ws = XLSX.utils.json_to_sheet(JSON.parse(str));
and file downloaded is still corrupt and the console is like this:

Copy and paste that string into your application and store it as some variable (like datajson) then hardcode downloadResData(JSON.parse(datajson)) and see if the problem shows up locally.
Finally, fixed the issue. The problem was with the browserify package. The version I was using was way too old, I updated it to the latest one and got rid of the corrupted downloads.
Most helpful comment
Finally, fixed the issue. The problem was with the
browserifypackage. The version I was using was way too old, I updated it to the latest one and got rid of the corrupted downloads.