Home  >  Article  >  Web Front-end  >  AngularJS uses webApi to export data

AngularJS uses webApi to export data

小云云
小云云Original
2018-03-07 13:08:522060browse

This article mainly shares with you AngularJS using webApi to export data code examples. I hope the code in this article can help everyone.

 /////导出功能
    self.importExcel = function () {
        var dataUrl = "http://103.233.7.38:8090/API/_oa/ProjectInfo.asmx/Export";
        $http({
            method: 'post',
            url: dataUrl,
            data: { },
            transformRequest: function (data) {
                return $.param(data);
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            responseType: 'arraybuffer'
        }).success(function (data) {
            var blob = new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
            var da = new Date();
            var fileName = "数据导出" + da.getFullYear() + '-' + (da.getMonth() + 1) + "-" + da.getDate();
            self.saveas(blob, fileName);
        });
    };
    self.saveas = function (blob, fileName) {
        if (window.navigator.msSaveOrOpenBlob) { // For IE:
            navigator.msSaveBlob(blob, fileName+".xlsx");
        } else { // For other browsers:
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = fileName + ".xlsx";
            link.click();
            window.URL.revokeObjectURL(link.href);
        }
    }

Related recommendations:

Nginx solves WebApi cross-domain secondary request example

Let WebAPI return data in JSON format tutorial

Share WebApi2 file and image upload and download function examples

The above is the detailed content of AngularJS uses webApi to export data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn