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?
application/vnd.ms-excel