search

Home  >  Q&A  >  body text

angular.js - How does angular implement excel export through $http service?

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?

为情所困为情所困2755 days ago641

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福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);
        });
    }

    reply
    0
  • Cancelreply