首页 > AngularJS 如何下载服务端返回的附件

AngularJS 如何下载服务端返回的附件

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

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

Accept-Range:bytes
Connection:keep-alive
Content-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.xlsx
Date:Wed, 20 Apr 2016 09:39:33 GMT
Transfer-Encoding:chunked
X-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" class="btn" ng-click="export()">导出</button>

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


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

【热门文章】
【热门文章】