使用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' 呢?