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. ArcGIS 相同要素类的多Shp文件或多要素合并

  2. weX5如何绑定KO对象

    define(function(require){ var $ = require("jquery"); var justep = require("$UI/system ...

  3. Javascript中的window.event.keyCode使用介绍

    <body onkeydown=" alert(window.event.keyCode)"> <body onkeydown="if(window.e ...

  4. LA3485 Bridge[(辛普森自适应)微积分]

    做此题完全是为了练积分. [普通求导版] Select Code #include<cstdio> #include<cmath> using namespace std; t ...

  5. 【BZOJ1070】[SCOI2007]修车 费用流

    [BZOJ1070][SCOI2007]修车 Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的. ...

  6. Cocos2d-x学习之 整体框架描述

    1.Cocos2d-x 整体描述 和传统的游戏引擎一样,cocos2d-x作为一个2d的游戏引擎,其也有以下几大概念组成: 导演(CCDiretor): 在cocos2d-x引擎中,CCDirecto ...

  7. CH5402 选课【树形DP】【背包】

    5402 选课 0x50「动态规划」例题 描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了 N(N≤300) 门的选修课程,每个学生可选课程的数量 M 是 ...

  8. 更新openssl

    在安装nodejs或者nginx什么的时候,有时候会报如下错误 npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.s ...

  9. 【react表格组件】material-table 基本用法 & 组件override

    教程: https://mbrn.github.io/material-table/#/ https://material-ui.com/api/table/ github: https://gith ...

  10. Divisibility by Eight---cf550C(被8整除 暴力)

    题目链接:http://codeforces.com/problemset/problem/550/C 题意是给你一个不操过100位的数,问删除m位之后,问剩下的数不改变顺序能被8整除的数存在不存在: ...