angular.js - angular怎么通过$http服务实现excel导出
为情所困
为情所困 2017-05-15 17:08:07
0
1
637

前后端分离,前端通过ng的$http传post到后端,后端返回responsetype为application/vns.ms-excel的数据,请问有什么方式才能让浏览器下载excel文件呢?

为情所困
为情所困

Antworte allen(1)
仅有的幸福
// 创建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);
    });
}
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage