我正在尝试使用NodeJS / express 4创建一个端点,该端点生成并向用户发送xlsx文件.
要创建xlsx文件,我使用的是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();
我试图通过Angular应用程序下载此文件.
$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);
但是,正在下载的文件已损坏,因此无法打开它.