angular.js - How does angular implement excel export through $http service?
为情所困
为情所困 2017-05-15 17:08:07
0
1
638

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?

为情所困
为情所困

reply all(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);
    });
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template