import fileDownload from 'js-file-download'

let params = {
"token" : "",
"id" : "",
"filename":"123.rar",
"pckid":"",
"dty":"tbox",
"cml":"",
"version":"",
};
let that = this;
API.test2(params).then(function (result) {
if (result) {
// console.log(result);
/*console.log(result.headers['Content-Disposition']);
let filename = result.headers['Content-Disposition'].substring(result.headers['Content-Disposition'].indexOf("=")+1 );
console.log("filename",filename);*/
fileDownload(result,"123.rar");
/*let blob = new Blob([result], {type: "application/octet-stream"});
       let objectUrl = URL.createObjectURL(blob);
       window.location.href = objectUrl;*/
}
}).catch(function (error) {
that.$message.error({showClose: true, message: '请求出现异常', duration: });
});
 test2:params => {
return API.POST2(`apitbox/download`,params,{responseType: 'blob'})//{responseType: 'blob'}一定要加,否则文件出错
},
export const POST2 = (url, params,config) => {
return axios.post(`${base}${url}`, params,config).then(res => res.data).catch(function (error) {
alert("请求出现异常");
console.log(error);
// window.location.reload();
});
}

后台springboot:

    // 文件下载相关代码
@RequestMapping(value = "/download", method = { RequestMethod.POST, RequestMethod.GET }) // postman,url,3.tbox请求下载文件,暂时只支持单文件下载。
public String downloadFile(@RequestBody Map<String, String> reqMap, HttpServletRequest request, HttpServletResponse response) { // version是路径
String token1 = reqMap.get("token");// request.getParameter("token");
String uuid1 = reqMap.get("id");// request.getParameter("uuid");
String fileName = "upload\\" + reqMap.get("dty") + "\\" + reqMap.get("cml") + "\\" + reqMap.get("version") + "\\" + reqMap.get("filename"); String name = fileName.substring(fileName.lastIndexOf("\\") + 1); try {
apiTboxService.saveDownloadfile(reqMap.get("pckid"), uuid1, name, CommomUtil.DateFormat(), "download packages start",CommomUtil.servernum);
} catch (Exception e1) {
e1.printStackTrace();
} if (fileName != null) {
// 设置文件路径
/* String realPath = request.getServletContext().getRealPath("//WEB-INF//"); */
String realPath = request.getSession().getServletContext().getRealPath("/");
File file = new File(realPath, fileName);
if (file.exists()) {
response.setContentType("application/force-download");//
response.setHeader("content-type", "application/octet-stream");
response.addHeader("Content-Disposition", "attachment;fileName=" + name);// 设置文件名
byte[] buffer = new byte[5*1024 * 1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("--------------download----------------success");
try {
apiTboxService.updateDownloadfile(reqMap.get("pckid"), uuid1, "success");
} catch (Exception e1) {
e1.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("download---error");
try {
apiTboxService.updateDownloadfile(reqMap.get("pckid"), uuid1, e.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}

vue下载文件的更多相关文章

  1. 前端vue下载文件时blob返回流中怎么获取文件名

    我很久之前写了一篇前端vue利用blob对象下载文件,有些人私信我,如果后端返回流失败,给出的json对象该怎么获得?前端获取的流怎么能获取原文件名?其实在那篇文章之后,我就已经针对这两个问题进行了优 ...

  2. vue 上传文件 和 下载文件

    Vue上传文件,不必使用什么element 的uplaod, 也不用什么npm上找的个人写的包,就用原生的Vue加axios就行了, 废话不多说,直接上代码:html: <input type= ...

  3. vue 上传文件 和 下载文件 面试的时候被问到过

    Vue上传文件,不必使用什么element 的uplaod, 也不用什么npm上找的个人写的包,就用原生的Vue加axios就行了, 废话不多说,直接上代码:html: <input type= ...

  4. springboot整合vue实现上传下载文件

    https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953 文章目录 springboot整合vue实现上传下载文件 1上传下载文件api文 ...

  5. 使用vue+iview实现上传文件及常用的下载文件的方法

    首先说明一下,我们这次主要用的还是iview的upload上传组件,下面直接上代码 <Upload ref="upload" multiple='true' //是否支持多文 ...

  6. vue+axios下载文件的实现

    HTML代码: <el-button size="medium" @click="download">下载表格</el-button> ...

  7. vue通过Blob实现下载文件

    需求是这样的...... 具体实现,前端拿到后端返回回来的数据,然后通过Blob实现下载,文件内容样式啥的都是后端写的 script代码: 这里的data就是后端返回回来的数据,此方法兼容IE dow ...

  8. vue axios post请求下载文件,后台springmvc完整代码

     注意请求时要设置responseType,不加会中文乱码,被这个坑困扰了大半天... axios post请求:     download(index,row){         var ts =  ...

  9. js之Ajax下载文件

    传统上,客户端将依靠浏览器来处理从服务器下载文件.然而,这种方法需要打开一个新的浏览器窗口,iframe或任何其他类型的不友好和黑客行为.为下载请求添加额外的头信息也很困难.更好的解决方案是使用HTM ...

随机推荐

  1. STL 源代码剖析 算法 stl_algo.h -- inplace_merge

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...

  2. JZOJ.5326【NOIP2017模拟8.21】LCA 的统计

    Description

  3. sql语句判断身份证性别等

    SELECT t.card_number ,) AS "省份", SUBSTR(t.card_number,,) "出生年月", SUBSTR(t.card_n ...

  4. Servlet3.0文件上传

    Servelt3.0文件上传作为一种便捷的文件上传方式很是值得我们去应用的 1.Servlet3.0文件上传使用步骤 浏览器端的要求 表单的提交方法必须是post 必须有一个文件上传组件 <in ...

  5. Tomcat----->tomcat配置虚拟主机(搭建网站)mac

    1.首先在server.xml中添加HOST <Host name="www.snowing.com" appBase="/Users/snowing/Downlo ...

  6. 2017-2018-2 20165330 实验四《Android程序设计》实验报告

    下载与安装Android Studio 下载地址:Download Android Studio 安装教程参考Android开发简易教程 实验内容 码云链接 任务一 参考<Java和Androi ...

  7. 2017-2018-2 20165330 实验三《敏捷开发与XP实现》实验报告

    实验内容 P基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件开发:即将软件需求分析.软件设计.软件构建.软件测试和软件维护这些相关技术和过程统一到一个体系中 敏捷开发:是一种以人为核 ...

  8. linux tree命令以树形结构显示文件目录结构

    http://jingyan.baidu.com/article/acf728fd19c7eff8e510a3eb.html  winscp  传递文件到ubuntu上用winscp

  9. Python--格式化输出%s和%d

    https://www.cnblogs.com/claidx/p/7253288.html pythn print格式化输出. %r 用来做 debug 比较好,因为它会显示变量的原始数据(raw d ...

  10. Web项目管理工具精选(下)

    原文:Web项目管理工具精选(下) 我们在上篇中已推介『代码管理.任务管理.支付工具.数据记录.Dashboard Analytics.客户支持』六个方面的工具.本文将介绍剩下七类工具. A/B测试 ...