3

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:

   $http({
            method: 'POST',
            url: API_URL + 'api/biApp/common/getXls',
            data:options,
            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);
            // window.open(objectUrl);
        }).error(function (data, status, headers, config) {
                //error handling...
        });

what am I doing wrong?

1
  • I remember I had a problem like this, I had to send the data from the server side as a file, my server side was ruby on rails and angularjs as front end, please check this paste.ubuntu.com/23722396 and tell me if it makes any difference. also change the content type to application/vnd.ms-excel Commented Jan 1, 2017 at 15:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.