downloading an excel file from Node.js back-end using angular.js front-end


I am trying to download an excel file from a node.js server.

when I open the requested file on the server side it is just ok, but when I am opening the downloaded file on the client side it is corrupted (the file format or file extension is not valid).

my server side code:

   var file = fs.readFileSync(path.join(__dirname, '../../') + tempFile, 'binary');
                    res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                    res.setHeader('Content-Disposition', "attachment; filename=" + "out.xlsx")
                    res.end(file, 'binary');

my angular-service:

            method: 'POST',
            url: API_URL + 'api/biApp/common/getXls',
            headers:{'Accept': "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
            responseType: "arraybuffer"
        }).success(function (data, status, headers, config) {
            var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
            saveAs(blob, "excel.xlsx");
            //I have tried also:
            // var objectUrl = URL.createObjectURL(blob);
        }).error(function (data, status, headers, config) {
                //error handling...

what am I doing wrong?

Show source
| angularjs   | node.js   2017-01-01 15:01 0 Answers

Answers ( 0 )

◀ Go back