zoukankan      html  css  js  c++  java
  • 使用ajax方式下载文件

    先下载jquery.fileDownload.js

     引入js;

    js代码:

    var url = "${pageContext.request.contextPath}/exportExcel/exportMemberFile";
                //实现Ajax下载文件
                $.fileDownload(url, {
                    httpMethod : 'POST',
                    data : $("#form1").serialize(),
                    prepareCallback : function(url) {debugger
                        $("#exportBtn").attr("disabled", true);
                        $("#exportBtn").html("正在导出");
                    },
                    successCallback : function(url) {debugger
                        $("#exportBtn").attr("disabled", false);
                        $("#exportBtn").html("导出");
                    },
                    failCallback : function(html, url) {
                        alert("导出失败");
                    }
                });

    后台response要进行如下设置;

    response.setContentType("application/x-download");
            response.setCharacterEncoding("utf-8");// 处理编码问题
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xls");// 表头编码问题
            Cookie fileDownload=new Cookie("fileDownload", "true");
            fileDownload.setPath("/");
            response.addCookie(fileDownload);

    然后就可以用了

  • 相关阅读:
    java返回json数据日期为一串数字字符串 js 转义
    ==和equals以及hashcode
    【线程分析】
    【dubbo&zookeeper】
    线程安全实现方案
    IOC原理
    java锁
    java特殊运算符
    HashMap原理和TreeMap原理
    volatile与synchronized
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/10782872.html
Copyright © 2011-2022 走看看