angularjs 服务端下载execl

2021-01-14 19:11

阅读:722

标签:href   download   out   action   disable   int   move   har   child   

html

button class="btn btn-sm btn-primary pull-right" ng-click="exportData($event)">i class="fa fa-spinner fa-pulse hidden">i>导出表格button>

js

//导出数据
        $scope.exportData = function(e) {
            var arr = e.target.children[0].className.split(‘ ‘);
            var index = arr.indexOf(‘hidden‘);
            arr.splice(index, 1);
            e.target.children[0].className = arr.join(‘ ‘);
            e.target.disabled = true;

            function resetBtn(e) {
                e.target.children[0].className = e.target.children[0].className + ‘ hidden‘;
                e.target.disabled = false;
            }

            $http({
                method: ‘get‘,
                url: $scope.baseUrl + ‘sampleCenter/reception?part=seek&action=exportExcel‘,
                responseType: ‘arraybuffer‘
            }).then(function(response) {
                if (response.headers()[‘content-type‘] == ‘text/html; charset=utf-8‘) {
                    var encode = new TextDecoder(‘utf-8‘);
                    var data = JSON.parse(encode.decode(new Uint8Array(response.data)));
                    if (data.error) {
                        toaster.pop({
                            type: ‘error‘,
                            body: data.error,
                            timeout: 0
                        });
                        resetBtn(e);
                    }
                } else {
                    var blob = new Blob([response.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
                    var objectUrl = URL.createObjectURL(blob);
                    var a = document.createElement(‘a‘);
                    a.href = objectUrl;
                    a.target = ‘_blank‘;
                    a.download = ‘表格‘;
                    document.body.append(a);
                    a.click();
                    a.remove();
                    resetBtn(e);
                }
            }, function() {
                toaster.pop({
                    type: ‘error‘,
                    body: ‘下载失败请与管理员联系!‘,
                    timeout: 0
                });
                resetBtn(e);
            })
        }

 

angularjs 服务端下载execl

标签:href   download   out   action   disable   int   move   har   child   

原文地址:https://www.cnblogs.com/yiyangl/p/13406164.html


评论


亲,登录后才可以留言!