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. JS-Zepto.js中文链接

    附上zepto.js的中文链接:http://www.css88.com/doc/zeptojs_api/ 小伙伴再也不用担心“这特么到底啥意思!”

  2. 【BZOJ3120】Line 矩阵乘法

    [BZOJ3120]Line Description Wayne喜欢排队……不对,是Wayne所在学校的校长喜欢看大家排队,尤其是在操场上站方阵.某日课间操时,校长童心大发想了一个极具观赏性的列队方案 ...

  3. Max_connect_errors – MySQL性能参数详解

    转载http://blog.csdn.net/wulantian/article/details/9670957 ax_connect_errors是一个MySQL中与安全有关的计数器值,它负责阻止过 ...

  4. [Android Tips] 26. Multiple Maven repositories in Gradle

    来自 https://gradleproject.wordpress.com/2013/02/14/multiple-maven-repositories-in-gradle/ This DOESN' ...

  5. 5501环路运输【(环结构)线性DP】【队列优化】

    5501 环路运输 0x50「动态规划」例题 描述 在一条环形公路旁均匀地分布着N座仓库,编号为1~N,编号为 i 的仓库与编号为 j 的仓库之间的距离定义为 dist(i,j)=min⁡(|i-j| ...

  6. FormsAuthentication 使用指南

    配置安全鉴别 鉴别是指鉴定来访用户是否合法的过程.ASP.NET Framework支持三种鉴别类型: Windows鉴别: NET Passport鉴别: Forms鉴别. 对于某一特定的应用程序, ...

  7. (转)聊聊Servlet、Struts1、Struts2以及SpringMvc中的线程安全

    前言 很多初学者,甚至是工作1-3年的小伙伴们都可能弄不明白?servlet Struts1 Struts2 springmvc 哪些是单例,哪些是多例,哪些是线程安全? 在谈这个话题之前,我们先了解 ...

  8. RESTful HTTP的实践(转)

    add by zhj: 文章有点老了,2009年的,到现在已经六年了,不过还是很有参考价值的. 另外,吐槽一下PUT method,竟然允许用户用实例号来创建,靠,这也行,实例号还是后台来定义比较方便 ...

  9. virtio前端驱动详解

    2016-11-08 前段时间大致整理了下virtIO后端驱动的工作模式以及原理,今天就从前端驱动的角度描述下目前Linux内核代码中的virtIO驱动是如何配合后端进行工作的. 注:本节代码参考Li ...

  10. 0701-spring cloud config-简介、Config Server开发、Config Client开发

    一.概述 参看地址: https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_spring_ ...