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.

Corrupt XLSX file after downloading

See original GitHub issue

Hi, I know there is already a very similar issue here but it does not provide a solution, because the author of this issue just uses a different approach to download the file in the end. https://github.com/SheetJS/js-xlsx/issues/122

I’m trying to download a file which is generated in the nodejs backend from the angular frontend, but when I try to open it, it’s always corrupted. Of course I have seen the example in the readme of this project and tried it, but it also came out corrupted. I can write the output to a file and then point the browser to it by using window.location() and the file is fine. Sadly I could not get this approach working with my authorization code. I read somewhere that it’s better to send buffer objects when working with expressjs, so here is my code right now:

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

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

        res.writeHead(200, [['Content-Type',  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']]);
        res.end( wbout );

// Frontend
// This triggers the backend function:
 $http.get("/abrechnung/exportToExcel/" + JSON.stringify(pageAndFilter.filterByFields)).
             success(function(data, status, headers, config) {  
              // The data object is the buffer
        saveAs(new Blob([data],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), "test.xlsx");
        }).
  error(function(data, status, headers, config) {
  });

Any help would be greatly appreciated!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:27 (6 by maintainers)

github_iconTop GitHub Comments

69reactions
nikolaifischercommented, May 5, 2015

Ok. Sorry for the monologue but I solved it: In the frontend when making the GET Request, the Response Type has to be set to arraybuffer like this:

$http({method: 'GET', url: "/abrechnung/exportToExcel/" + JSON.stringify(pageAndFilter.filterByFields) ,
        headers: {'Content-Type': "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, responseType: "arraybuffer"}).             
             success(function(data, status, headers, config) {  
             // continue like before...

I think this is actually an issue in angularJS and has nothing to do with this project. Thank you anyways!

38reactions
nikolaifischercommented, May 8, 2015

I hope this helps! Feel free to use or edit it as you wish

// Backend:
// Be sure to write the workbook in the type:'buffer'
var wopts = { bookType:'xlsx', bookSST:false, type:'buffer' };
var wbout = XLSX.write(workbook,wopts);     
// Send the buffer:
res.end( wbout );

// AngularJS Frontend:
exportToExcel: function() {

    // You have to use http GET, otherwise browsers will not download the excel
    $http({method: 'GET', url: "/yourAPI/exportToExcel/",
        // This is important! Tell it to expect an arraybuffer
        responseType: "arraybuffer"}).             
        success(function(data, status, headers, config) {  
            // use the saveAs library to save the buffer as a blob on the user's machine. Use the correct MIME type!
            saveAs(new Blob([data],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}), "excel.xlsx");
        }).
        error(function(data, status, headers, config) {

        });  
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Open Corrupt Excel XLS and XLSX Files?
Click Browse to select the corrupt Excel XLS or XLSX file. The File Open window will pop on-screen now; select the corrupt Excel...
Read more >
Corrupt XLSX file after downloading - Stack Overflow
ok, try decompressing the file (treat it as a .zip) if it works, it means it is not corrupt but there was a...
Read more >
Get rid of "The file is corrupt and cannot be opened" in Excel
How to open a corrupt xls. file in Excel 2010 - 365 · Open Excel. · Click on File -> Options. · Select...
Read more >
7 Ways to Repair Damaged and Corrupt Excel Files
To repair damaged and corrupt Excel files, you can just launch the MS Excel application and go to its Menu > File >...
Read more >
[Fixed] The File Is Corrupted and Cannot Be Opened in Excel ...
4 Fixes for "The file is corrupt and cannot be opened" in Excel/Word · Method 1. Repair Excel/Word File with Third-Party Software ·...
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