question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

xlsx file corrupted after download with Angular2

See original GitHub issue

EDIT: for more details, see the angular 2+ demo

Hi,

I’m working with Angular 2 on frontend and Express on backend.

I’ve succeeded in creating a workbook with js-xlsx on backend.

var wopts = {showGridLines: false};

XLSX.writeFile(wb, 'output.xlsx', wopts);

res.status(201).send();

With code above, I could find the created file in backend folder and successfully open it.

Now I’d like to allow client side to download this file. I modified my backend code as following :

var wopts = {
    bookType: 'xlsx',
    bookSST: false,
    type: 'buffer',
    showGridLines: false
};

var wbout = XLSX.write(wb, wopts);

res.end(wbout);

And on Angular 2 frontend, I have following code with FileSaver.js injected :

var headers = new Headers();

headers.append('Content-Type', 'application/json');

headers.append('responseType', 'arraybuffer');

this.authHttp.post('http://localhost:3001/createExcel', body, { headers: headers })
  .subscribe(
  response => {
    saveAs(new Blob([response._body], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }), "output.xlsx");
  },
  error => {
    alert(error.text());
    console.log(error.text());
  }
  );

Downloading is working well, but I can not manage to open it, with information :

We found a problem with some content in ‘output.xlsx’. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.

Then

The file is corrupt and cannot be opened.

Any help will be appreciated, thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
zajjosephcommented, Jul 4, 2016

I made it working by creating xlsx file with js-xlsx on client side and then directly download it, without any interaction with the server.

1reaction
zajjosephcommented, Feb 8, 2017

@junaidinam @zh-wowtv var saveAs = require('file-saver'); var wopts = { bookType: 'xlsx', bookSST: false, type: 'binary', showGridLines: false };

var wbout = XLSX.write(wb, wopts);

saveAs(new Blob([this.s2ab(wbout)], { type: '' }), 'test.xlsx');

s2ab(s: any) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); for (var i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; }

Read more comments on GitHub >

github_iconTop Results From Across the Web

xlsx file corrupted after download with Angular2 #433 - GitHub
_body], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }), "output.xlsx"); }, error => { alert(error.text()) ...
Read more >
Angular2 download excel file from Web API, file is corrupt
alertAppService.generateMonthlySpreadsheet(header).then(function (data){ var blob = new Blob([data], {type: "application/vnd.openxmlformats- ...
Read more >
[Solved]-Angular 2 downloading a file: corrupt result-angular.js
[Solved]-Angular 2 downloading a file: corrupt result-angular.js ... You still need to add the "Accept" header in the request as answered by Chris....
Read more >
Unable to download excel file from src assets in development ...
Excel cannot open the file "sample.xlsx" because the file format or file extension is invalid, according to the error message. Check to make ......
Read more >
WebApi Core Export to Excel, return Angular as download ...
Excel cannot open the file because the file XXX-8_3_2021.xlsx format or file extension is not valid. Verify that the file has not been...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found