angular.js - AngularJS 如何下载服务端返回的附件

【字号: 日期:2023-01-15浏览:21作者:雯心

问题描述

使用AngularJS $http向nodejs服务端发送导出数据请求时,得到的excel文件是乱码,控制台输出response.data如下图:angular.js - AngularJS 如何下载服务端返回的附件

服务端返回的头部信息如下:

Accept-Range:bytesConnection:keep-aliveContent-Disposition:attachment; filename=%E8%A1%A5%E8%B4%B4%E8%B5%84%E6%A0%BC%E5%B7%B2%E5%AE%A1%E6%A0%B8%E4%BF%A1%E6%81%AF%E8%A1%A8.xlsxDate:Wed, 20 Apr 2016 09:39:33 GMTTransfer-Encoding:chunkedX-Powered-By:Express

Angular controller:

$scope.export = function() { $http({url: '/api/exportExcel',method: 'GET' }).then(function(response) {var anchor = angular.element(’<a/>’);anchor.attr({ href: ’data:attachment/xlsx;charset=utf-8,’ + encodeURI(response.data), target: ’_blank’, download: ’2016-04-20.xlsx’})[0].click(); });}

Angular view:

<button type='button' ng-click='export()'>导出</button>

请问如何得到正确可用的excel文件?像这种导出数据到excel的普遍实现方法是什么? 感谢!

问题解答

回答1:

试试直接使用 $window.location = ’/api/exportExcel’ 呢?

相关文章: