1传统的ajax虽然能提交到后台,但是返回的数据被解析成json,html,text等字符串,无法响应浏览器下载。就算使用bob模拟下载,数据量大时也不方便

废话不多说:上代码(此处是Layui监听提交,传统的html提交更方便)

//监听提交
form.on('submit(formDemo)', function (data) {
layer.msg(JSON.stringify(data.field));
//第一种方式 这种方式直接打开链接,参数后缀到后台查找,即可下载
// window.open('/jump/download?id='+data.field.id, '_blank');

//第二种方式 这种方式模拟表单提交,动态获取值传入后台
try {
var queryForm = $('#llll');
var exportForm = $("<form action=' /jump/download' method='post'></form>")

queryForm.find("input").each(function() {
var name = $(this).attr("name");
var value = $(this).val();
exportForm.append("<input type='hidden' name='" + name + "' value='" + value + "'/>")
});

queryForm.find("select").each(function() {
var name = $(this).attr("name");
var value = $(this).val();
exportForm.append("<input type='hidden' name='" + name + "' value='" + value + "'/>")
});

$(document.body).append(exportForm);
exportForm.submit();
} catch (e) {
console.log(e);
} finally {
exportForm.remove();
}

//3第三种ajax提交表单,不相应浏览器下载 这种方式可以提交一个Map 去后台但是返回文件不容易下载,精通原生js的伙伴可以试试,比较麻烦,再次我就不献丑了
// $.ajax({
// type:"GET",
// url:"/jump/download",
// data:JSON.stringify(data.field),
// contentType:"application/json;charset=UTF-8",
// success:function (data) {
//
// }
// });
return false;//ajax 提交时Layui防止页面自动刷新
});

 @ApiOperation(value = "导出word文档", notes = "{\"id\":\"11\"}")
@RequestMapping(value = "/download",method = RequestMethod.POST)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {

//对应前端第三种提交方式 @RequestBody Map map 第一种方式支持前端提交过来一个Map
Map<String, Object> userMapMeS = new HashMap<String, Object>();
// String fileName=map.get("name").toString();
// userMapMeS.put("name", map.get("name").toString());
// userMapMeS.put("id", map.get("id").toString());
// userMapMeS.put("age", map.get("age").toString());
// userMapMeS.put("phone", map.get("phone").toString());
// userMapMeS.put("password", map.get("password").toString());
// userMapMeS.put("email", map.get("email").toString());
// userMapMeS.put("times", map.get("times").toString());
// userMapMeS.put("address", map.get("address").toString());
// userMapMeS.put("sex", map.get("sex").toString());
// userMapMeS.put("work", map.get("work").toString());
// userMapMeS.put("hobby", map.get("hobby").toString());
//对应第一种前端提交方式

// String id=request.getParameter("id");
// Map map=new HashMap();
// map.put("id",id);
// User user= userService.selectUser(map);
// String fileName=user.getName();
// Map<String, Object> userMapMeS = new HashMap<String, Object>();
// userMapMeS.put("name", user.getName());
// userMapMeS.put("id", user.getId());
// userMapMeS.put("age", user.getAge());
// userMapMeS.put("phone", user.getPhone());
// userMapMeS.put("password", user.getPassword());
// userMapMeS.put("email", user.getEmail());
// userMapMeS.put("times", user.getTimes());
// userMapMeS.put("address", user.getAddress());
// userMapMeS.put("sex", user.getSex());
// userMapMeS.put("work", user.getWork());
// userMapMeS.put("hobby", user.getHobby());

//对应前端第二种前端模拟表单提交
String fileName =request.getParameter("name").toString();
userMapMeS.put("name", request.getParameter("name").toString());
userMapMeS.put("id", request.getParameter("id").toString());
userMapMeS.put("age", request.getParameter("age").toString());
userMapMeS.put("phone", request.getParameter("phone").toString());
userMapMeS.put("password", request.getParameter("password").toString());
userMapMeS.put("email", request.getParameter("email").toString());
userMapMeS.put("times", request.getParameter("times").toString());
userMapMeS.put("address", request.getParameter("address").toString());
userMapMeS.put("sex", request.getParameter("sex").toString());
userMapMeS.put("work", request.getParameter("work").toString());
userMapMeS.put("hobby", request.getParameter("hobby").toString());
String endCodeFileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
response.reset();//清除空白行纺织报错
response.setHeader("Access-Control-Allow-Origin", "*");//所有域都可以跨
response.setHeader("Content-Type", "application/octet-stream;charset=UTF-8");//二进制 流文件
response.setHeader("Content-Disposition", "attachment;filename=" + endCodeFileName+".doc");//下载及其文件名
response.setHeader("Connection", "close");//关闭请求头连接
//设置文件在浏览器打开还是下载
response.setContentType("application/x-download");

WordUtil wUtil = new WordUtil();
long now= System.currentTimeMillis();
wUtil.createDoc(userMapMeS, response.getOutputStream());
long end= System.currentTimeMillis();
long ddd=end-now;
System.out.println("ddd" + ddd);

}
                    工具类在下篇文章

前端调用后台接口下载word文档的两种方法的更多相关文章

  1. .NET通过调用Office组件导出Word文档

    .NET通过调用Office组件导出Word文档 最近做项目需要实现一个客户端下载word表格的功能,该功能是用户点击"下载表格",服务端将该用户的数据查询出来并生成数据到Word ...

  2. Java导出freemarker实现下载word文档格式功能

    首先呢,先说一下制作freemarker模板步骤, 1. 在WPS上写出所要的下载的word格式当做模板 2. 把模板内不固定的内容(例:从数据库读取的信息)写成123或者好代替的文字标注 3. 把固 ...

  3. ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)

    ASP.NET实现在线浏览Word文档另一种解决方案(Word转PDF)      上述博文里提到的在线浏览pdf的方案不错,但word转pdf的那个dll只支持doc不支持docx,附上最新的下载链 ...

  4. 前端调用后端接口下载excel文件的几种方式

    今天有一个导出相应数据为excel表的需求.后端的接口返回一个数据流,一开始我用axios(ajax类库)调用接口,返回成功状态200,但是!但是浏览器没有自动下载excel表,当时觉得可能是ajax ...

  5. PHP生成word文档的三种实现方式

    PHP生成word原理 利用windows下面的 com组件 利用PHP将内容写入doc文件之中 具体实现: 利用windows下面的 com组件 原理:com作为PHP的一个扩展类,安装过offic ...

  6. 将html转换为word文档的几种方式

    1 基于wps直接将页面信息下载成word文档 public void test() { WPS.Application wps = null; try { wps = new WPS.Applica ...

  7. DEDECMS5.5/5.6/5.7列表页调用TAG标签(热门标签)的两种方法

    DEDECMS5.5/5.6/5.7列表页调用TAG标签的两种方法: 一.DedeCMSv5.6及其以前版本: dedecms默认在列表是无法调用tag标签的,经过各位版主们的帮助,现给大家提供出2种 ...

  8. 使用PHP对word文档进行操作的方法

    使用php时,因为加密等原因,如果直接用FILE后者OPEN等函数读取WORD的话往往是乱码,原来要使用COM 这是我简单的一个读取并存储到新的WORD上的文件<? // 建立一个指向新COM组 ...

  9. C# 打印PDF文档的10种方法

    操作PDF文档时,打印是常见的需求之一.针对不同的打印需求,可分多种情况来进行,如设置静默打印.指定打印页码范围和打印纸张大小.双面打印.黑白打印等等.经过测试,下面将对常见的几种PDF打印需求做一些 ...

随机推荐

  1. Leetcode:面试题28. 对称的二叉树

    Leetcode:面试题28. 对称的二叉树 Leetcode:面试题28. 对称的二叉树 Talk is cheap . Show me the code . /** * Definition fo ...

  2. Redis介绍及使用(八)

    一.什么是Redis 1.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库.(非关系型缓存数据库) 2.五种数据类型: 3.支持持久化. 参看链接:https:// ...

  3. BigDecimal之除不尽报错

    当bigdecimal除不尽(循环小数)后会报错,下面的是BigDecimal ,divide方法提供的精确小数方法(推荐使用) BigDecimal avgCapital = loanAmount. ...

  4. PGSQL基础语句汇总

    一.pgsql里面的数据类型不再介绍:https://www.runoob.com/postgresql/postgresql-data-type.html 二.常用基本语句 2.1.CREATE D ...

  5. odoo中Controller

    一:Controller 一般通过继承的形式来创建controller类,继承自odoo.http.Controller. 以route装饰器来装饰定义的方法,提供url路由访问路径: class M ...

  6. Mysql用户、权限、密码管理

    一.用户管理 默认:用户root 创建用户: use mysql; create user 'alex'@'192.168.193.200' identified by '123456'; 创建了al ...

  7. 【系统学习ES6】第一节:新的声明方式

    [系统学习ES6] 本专题旨在对ES6的常用技术点进行系统性梳理,帮助大家对其有更好的掌握.计划每周更新1-2篇,希望大家有所收获. 以前用ES5时,声明变量只能用var.ES6的出现,为我们带来了两 ...

  8. Java大整形BigInteger的用法

    基本类型int有32位,范围是:[-2147483648, 2147483647](正负21亿多) 基本类型long有64位,范围是:[-9223372036854775808, 9223372036 ...

  9. a href="tel" 拨打电话

    电话号码是固定的: <a href="'tel:10086">10086</a> 电话号码是动态获取时: 走默认的方式失败 <a href=" ...

  10. NAR | 张勇洪/周超/刘小云团队合作揭示2-羟基异丁酰化修饰调控光暗适应性反应机制

    景杰生物 | 报道 ​ 组蛋白赖氨酸的翻译后修饰是表观遗传学密码的重要组成部分,它们动态地调节染色质的结构和功能,影响基因表达活性,参与生物体的环境适应性调控.赖氨酸酰化修饰家族(Acylation) ...