ajax方式下载文件附带loading效果

ajax方式下载文件附带loading效果

<button type="button" class="btn btn-primary" οnclick="doexportexcleAjax()">ajax导出</button>

function doexportexcleAjax() {
            jQuery('#mainList').showLoading();
              var url = '${ctx}/bus/bianzhi/export/exportAjax';
              var xhr = new XMLHttpRequest();
              xhr.open('GET', url, true);    // 也可以使用POST方式,根据接口
              xhr.responseType = "blob";  // 返回类型blob
              // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
              xhr.onload = function () {
                  jQuery('#mainList').hideLoading();
                // 请求完成
                if (this.status === 200) {
                  // 返回200
                  var blob = this.response;
                  var reader = new FileReader();
                  reader.readAsDataURL(blob);  // 转换为base64,可以直接放入a表情href
                  reader.onload = function (e) {
                    // 转换完成,创建一个a标签用于下载
                    var a = document.createElement('a');
                    a.download = '编制信息.xls';
                    a.href = e.target.result;
                    $("body").append(a);  // 修复firefox中无法触发click
                    a.click();
                    $(a).remove();
                  }
                }
              };
              // 发送ajax请求
              xhr.send()
            }

 

/**
     * Ajax导出编制基本信息
     */
    @RequestMapping("exportAjax")
    public void exportAjax(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        biazhiExportService.exportBianzhiHardlistsAjax(request, response);
    }

 

/**
     * 导出编制基本信息
     * 
     * @param request
     * 
     * @param response
     * @param busPeopleBasePO
     */
    public void exportBianzhiHardlistsAjax(HttpServletRequest request, HttpServletResponse response) {
        List<BusBianzhiDTO> findList = bianzhiDTODao.findList(new BusBianzhiDTO());
       

        if (!findList.isEmpty()) {
            String rootPaths = this.getClass().getResource("/").getPath().replaceFirst("/WEB-INF/classes/", "/static/");
            rootPaths = rootPaths.substring(1, rootPaths.length());// 根路径
            String exclePath = rootPaths + "textExcle" + IdGen.uuid() + "/";
            String downPath = "";
            String downName = "";
            Map<String, Object> map = new HashMap<>();
            File src = new File(exclePath);
            if (!src.exists()) {// doc所在文件夹不存在
                src.mkdirs();// 创建文件夹
            }
            downName = "编制信息.xls";
            downPath = exclePath + downName;
            map.put("downName", downName);
            map.put("downPath", downPath);
            map.put("bianzhiList", findList);
            FreemarkerWord.exportBianzhiExcel(map, "bianzhidaochumuban");

            File f = new File(downPath);
            FileInputStream inputStream = null;
            OutputStream outPutStream = null;
            try {
                response.setContentType("application/ms-excel");
                response.setHeader("Content-disposition", "attachment; filename=" + new String(downName.getBytes("utf-8"), "ISO8859-1"));

                inputStream = new FileInputStream(f);
                outPutStream = response.getOutputStream();
                byte[] b = new byte[2048];
                int length;
                while ((length = inputStream.read(b)) > 0) {
                    outPutStream.write(b, 0, length);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 这里主要关闭。
                try {
                    outPutStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                deleteDir(exclePath);
            }

        } else {
            // 导出空的模板
            String appBasePath = request.getSession().getServletContext().getRealPath("/");
            String url = appBasePath + "/template/编制信息模板.xlsx";
            downTpl(url, response);
        }
    }

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遮罩用途及效果 ShowLoading这个jQuery插件设计用于当运行Ajax请求时,可以在屏幕某一特殊区域(id,class或者html标签)覆一张正在加载中的图片。 有时候我们页面调用后台程序时间比较长时,前台页面暴露在用户之下,如果用户点击可能会造成逻辑混乱。这时候,遮罩就起到了很好的效果,在触发后台程序时我们将前台页面遮住,不让操作,同时给予一个程序运行请等待的效果。 遮罩效果: 二、JQuery遮罩UI实现 <link href="style/showLoading.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/jquery.showLoading.min.js"></script> 三、使用方法 假设html页面有有一个class为add_test_img的标签,需要通过ajax访问后台,并在add_test_img标签中显示一些相关内容,在内容显示之前,可对add_test_img标签使用遮罩,防止在数据显示之前,被修改其中的内容 复制代码 //显示遮罩 $(".add_test_img").showLoading(); //ajax调用 $.ajax({ type: "GET", url: "Api.php", data: {ApiTarget: 'TestImage', op: 'paginateByClassId', prj_id: prj_id, class_id:class_id, page: index, per_page: items_per_page, user_id: user_id, start_date:start_date_str, end_date:end_date_str}, success: function(response){ //对返回数据进行处理,并显示 //...... //去除遮罩 $(".add_test_img").hideLoading(); }, error: function(xhr) { //显示失败信息 //...... //去除遮罩 $(".add_test_img").hideLoading(); } });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值