kennethjiang/js-file-download

Wrong encoding?

divined opened this issue ยท 8 comments

let xhr = new XMLHttpRequest();
xhr.open('GET', 'http://domain.com/some.xlsx', true);

xhr.onload = function(e) {
  if (this.status === 200) {
    fileDownload(this.response, 'export.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  }
};

xhr.send();

File downloaded, but wrong encoded, file opens as text inside xlsx -(

The same problem when i send file content from backend.
.xlsx file created with encoded content as text =(

What OS/browser are you on and their version? I will try to replicate it on my end.

Windows 10 / Chrom 61.0.3163.100

Anybody found a solution?

I solved some problem after add
xhr.responseType = 'blob';

Cool! Can you open a PR for that @fred-kruger ? Thank you so much!

maybe this is not the missing part about this library, just set responseType option in native xhr or related xhr library(eg: axios) to blob can solve this problem.

for sake of other people met the same problem, maybe just add troubleshooting section in README and linked to current issue is OK.

I've added something to the README to help people avoid this gotcha.