The front-end and back-end are separated. The front-end passes the post to the back-end through ng's $http, and the back-end returns data with response type application/vns.ms-excel. Is there any way to let the browser download the excel file?
仅有的幸福2017-05-15 17:10:07
// 创建a标签模拟下载
function exportExcel(params, filename) {
return $http({
url: '/api/exportExcel',
method: "POST",
headers: {
'Content-type': 'application/json'
},
params: params,
responseType: 'arraybuffer'
}).success(function (data) {
var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
var objectUrl = URL.createObjectURL(blob);
var a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display:none');
a.setAttribute('href', objectUrl);
a.setAttribute('download', filename);
a.click();
URL.revokeObjectURL(objectUrl);
});
}