使用angular.js前端从Node.js后端下载excel文件

时间:2017-01-01 14:53:18

标签: angularjs node.js

我正在尝试从node.js服务器下载excel文件。

当我在服务器端打开请求的文件时,它就可以了,但是当我在客户端打开下载的文件时,它已损坏(文件格式或文件扩展名无效)。

我的服务器端代码:

   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');

我的角度服务:

   $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...
        });

我做错了什么?

0 个答案:

没有答案