NodeJS / Express4 endpoint generates corrupt xlsx file

I am trying to create an endpoint using NodeJS / express 4 which generates and sends an xlsx file to the user.

To create an xlsx file, I use the library node-xlsx.

var xlsx = require('node-xlsx');
var buffer = xlsx.build([{
  name: pasta,
  data: data
}]);
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader("Content-Disposition", "attachment; filename=" + pasta + ".xlsx");
res.write(buffer, 'binary');
return res.end();

And I'm trying to download this file through an Angular app.

$http.post('https://endpoint/v1/' + folderName + '/reportExcel', {
    responseType: 'arraybuffer'
  })
  .success(function(response) {
      var blob = new Blob([response], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      });
      var objectUrl = URL.createObjectURL(blob);
      $window.open(objectUrl);

However, the file that is being downloaded is broken, so I cannot open it.

+4
source share
2 answers

Maybe this is because you are using the connect-livereload plugin? The plugin seems to be transferring corrupted binaries. I came across the same thing and decided it by adding “ignore” when starting the connect-livereload plugin.

app.use(require('connect-livereload')({
    ignore:['.xls', '.xlsx']
}));

. : https://github.com/intesso/connect-livereload/issues/39

+5

: fooobar.com/questions/1619984/...

, node-xlsx, base64 .

0

Source: https://habr.com/ru/post/1619982/


All Articles