Sheetjs: .xlsx file corrupt on download from express server but works fine on localhost server

Created on 24 Nov 2017  路  6Comments  路  Source: SheetJS/sheetjs

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");
  }
 }

Most helpful comment

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.

All 6 comments

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...

  1. So, I hardcoded the data like this and tested after deploying the code on heroku:
...
downloadResData() {
    let ws = XLSX.utils.json_to_sheet([{foo:1}, {bar:2}]);
...

and the file actually opened with the hardcoded data in it.

  1. Then, I reverted back the changes and put console logs everywhere and found out data being passed to the 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);
...

sheejs

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:

sheetjs2

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

magtuan picture magtuan  路  3Comments

mmancosu picture mmancosu  路  3Comments

goxr3plus picture goxr3plus  路  3Comments

gustavosimil picture gustavosimil  路  3Comments

jamespan0 picture jamespan0  路  3Comments