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. 如何使用odoo的compute方法,自动计算odoo字段

    前言 在odoo的ORM创建数据字段的过程中,我们会经常需要定义一些字段用来计算某一些字段只和或其他计算结果. 今天介绍一个很好用的方法compute计算属性,这个方法其实是属于写在odoo fiel ...

  2. 每天五分钟Go - 变量

    变量的声明 1.使用关键词 var 定义,声明后若不赋值,则使用默认值 var 变量名 [变量类型] [=初始值] var a,b,c string var e,f int = 0,1 声明时,如果省 ...

  3. 一文说清OpenCL框架

    背景 Read the fucking official documents! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: 对不 ...

  4. GC Roots包含哪些

    1.虚拟机栈中引用的对象 2.方法区中静态属性.常量引用的对象 3. 本地方法栈中引用的对象 4. 被Synchronized锁持有的对象 5. 记录当前被加载类的SystemDictionary 6 ...

  5. 学习笔记-CCS-MSP430F5529[快速入门篇一]

    由于最近竞赛需要使用TI公司的MSP430系列芯片,本人在最近两天匆忙的学习了一下MSP430F5529(下文统一称作5529)的使用.写下本文是为了将这两天学习内容做一个复习,并且将学习过程中遇到的 ...

  6. python算法(2)兔子产子(斐波那切数列)

    兔子产子 1.问题描述 有一对兔子,从出生后的第3个月起每个月都生一对兔子.小兔子长到第3个月后每个月又生一对兔子,假设所有的兔子都不死,问30个月内每个月的兔子总对数为多少? 2.问题分析 兔子产子 ...

  7. .NET Conf 2020大会将于2020年11月10日--- 11月12日举行 (UTC)时区

    .NET Conf 2020大会将于2020年11月10日--- 11月12日举行 (UTC)时区 开始时间 2020年11月10日 08:00 (PT) | 16:00 (UTC)| 24:00(北 ...

  8. 利用共享内存实现比NCCL更快的集合通信

    作者:曹彬 | 旷视 MegEngine 架构师 简介 从 2080Ti 这一代显卡开始,所有的民用游戏卡都取消了 P2P copy,导致训练速度显著的变慢.针对这种情况下的单机多卡训练,MegEng ...

  9. C++ //继承同名静态成员处理方式

    1 //继承同名静态成员处理方式 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 cl ...

  10. C++ 结构体案例2 升序打印数组中的元素

    1 //结构体案例 2 2 #include<iostream> 3 #include<string> 4 #include<ctime> 5 using name ...