简体   繁体   English

导出Excel时如何显示加载(处理中)?

[英]How to show loading(Processing) while export excel?

When export excel option is clicked, a call to the backend code will get the file( file_name.xls ) from server side.It gets downloaded through document.location.href 单击export excel选项后,对后端代码的调用将从服务器端获取文件( file_name.xls ),并通过document.location.href下载

jQuery("#exportExcel").click(function() {
                    $("#loading").show();
                    var exportExcelUrl='/portal/portalDownloadExcel?fileType=main';
                    document.location.href=exportExcelUrl;
                    $("#loading").hide();

        });

But above steps can't hide the processing image, because I don't get server's response. 但是上述步骤无法隐藏处理图像,因为我没有得到服务器的响应。

I have tried the ajax format to show loading , But file downloaded all the content has special characters. 我已经尝试过ajax格式来显示加载,但是文件下载的所有内容都有特殊字符。 To remove these special characters, i have used the : 'encodeURIComponent,blob,special characters remover' 要删除这些特殊字符,我使用了:'encodeURIComponent,blob,特殊字符删除器'

I use AngularJS and create specific service for reports: 我使用AngularJS并为报告创建特定的服务:

    app.factory('Report', function ($q, $sce, $http, API_LINK) {
  var report = {
    getTypes: function () {
      return [
        {name: 'html', title: 'HTML'},
        {name: 'pdf', title: 'PDF'},
        {name: 'excel', title: 'Excel'}
      ]
    },
    get: function (name, type, params, returnResult) {
      var html = type === 'html';
      params = params ? params : {};
      var p = {
        params: params,
        name: name,
        type: type
      };
      var url = API_LINK + 'report?' + $.param(p);

      var config = {
        method: 'GET',
        url: url
      };
      if (!html) {
        config.responseType = 'arraybuffer';
      }
      var http = $http(config);
      if (returnResult) {
        var deferred = $q.defer();
        http.error(function () {
          deferred.reject();
        });
      }
      http.success(
        function (data, status, headers) {
          if (returnResult) {
            return deferred.resolve($sce.trustAsHtml(data));
          }
          var type = headers('Content-Type');
          if (html) {
            var isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
            var win;
            if (isFirefox) {
              win = window.open('');
            } else {
              var params = ['height=' + screen.height, 'width=' + screen.width, 'fullscreen=yes'];
              win = window.open('', '_newtab', params.join(','));
            }
            if (win) {
              win.moveTo(0, 0);
              win.document.write(data);
              win.focus();
            } else {
              alert('Please allow popups for this site');
            }
          } else {
            var contentDisposition = headers('Content-disposition');
            var filename = 'Report ' + (new Date).toShortString() + ' ' + (new Date).toShortTimeString() + '.';
            if (type = 'application-pdf') {
              filename += 'pdf';
            }
            if (angular.isString(contentDisposition)) {
              filename = contentDisposition.split('"')[1];
            }
            var file = new Blob([data], {type: type});
            saveAs(file, filename);
          }
        });
      if (returnResult) {
        return deferred.promise;
      }
    }
  };
  return report;
});

Maybe this will help you 也许这对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM