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. gnome环境设置

    1.gnome的环境安装sudo apt-get install gnome2.选择gdm3 开机显示不正常:could not apply the stored configuration for ...

  2. PHP获取当日或本月时间戳范围

    在mysql数据库中使用int类型保存时间戳时,一般在程序中可能会用到统计当日,当月的一些数据.那么可以用如下的方式限定时间范围:   //当日销售 $today_start = strtotime( ...

  3. python之 数据类型限制

    问题增加类型限制 NameError: name 'List' is not defined def twoSum(self, nums: List[int], target: int) -> ...

  4. endnote x9.3.3 for windows安装教程

    EndNote X9.3.3 是一款非常nice的实用型文献管理软件,EndNote X9功能极其强劲,便捷好用.本文提供EndNote X9.3.3安装破解激活教程.方法,内附EndNote x9. ...

  5. LinkedHashMap 的实现原理

    LinkedHashMap 概述 HashMap 是无序的,HashMap 在 put 的时候是根据 key 的 hashcode 进行 hash 然后放入对应的地方.所以在按照一定顺序 put 进 ...

  6. decimal和float的区别

    场景 今天在开发的时候,在mongodb中有个字段保存的数据结构是decimal,然后需要对这个字段的值进行范围的查询.结果却怎么查询值范围都是空. 解决 如图中看到的,利用Navicat,可以明显的 ...

  7. GhostScript 沙箱绕过(命令执行)漏洞(CVE-2019-6116)

    影响范围 Ghostscript 9.24之前版本 poc地址:https://github.com/vulhub/vulhub/blob/master/ghostscript/CVE-2019-61 ...

  8. transform——转换

    ① 位移 transform:translate(x,y);     同时x轴和y轴的位移 translate(x);        在x轴上的位移 同 translateX(x) translate ...

  9. Jmeter分布式压测实战及踩坑处理(含参数化)

    项目中使用Jmeter进行大并发压测时,单机受限内存.CPU.网络IO,会出现服务器压力还没有上 去,但压测服务器由于模拟的压力太大死机的情况.JMeter的集群模式可以让我们将多台机器联合起来 一起 ...

  10. TypeScript学习笔记(一)环境搭建和数据类型

    目录 一.学习TypeScript的缘由 二.学习环境的搭建 1. TypeScript的编译环境 2. vscode自动编译的配置 三.TypeScript中的数据类型 1. 简单变量的定义和初始化 ...