vue下载文件
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下载文件的更多相关文章
- 前端vue下载文件时blob返回流中怎么获取文件名
我很久之前写了一篇前端vue利用blob对象下载文件,有些人私信我,如果后端返回流失败,给出的json对象该怎么获得?前端获取的流怎么能获取原文件名?其实在那篇文章之后,我就已经针对这两个问题进行了优 ...
- vue 上传文件 和 下载文件
Vue上传文件,不必使用什么element 的uplaod, 也不用什么npm上找的个人写的包,就用原生的Vue加axios就行了, 废话不多说,直接上代码:html: <input type= ...
- vue 上传文件 和 下载文件 面试的时候被问到过
Vue上传文件,不必使用什么element 的uplaod, 也不用什么npm上找的个人写的包,就用原生的Vue加axios就行了, 废话不多说,直接上代码:html: <input type= ...
- springboot整合vue实现上传下载文件
https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953 文章目录 springboot整合vue实现上传下载文件 1上传下载文件api文 ...
- 使用vue+iview实现上传文件及常用的下载文件的方法
首先说明一下,我们这次主要用的还是iview的upload上传组件,下面直接上代码 <Upload ref="upload" multiple='true' //是否支持多文 ...
- vue+axios下载文件的实现
HTML代码: <el-button size="medium" @click="download">下载表格</el-button> ...
- vue通过Blob实现下载文件
需求是这样的...... 具体实现,前端拿到后端返回回来的数据,然后通过Blob实现下载,文件内容样式啥的都是后端写的 script代码: 这里的data就是后端返回回来的数据,此方法兼容IE dow ...
- vue axios post请求下载文件,后台springmvc完整代码
注意请求时要设置responseType,不加会中文乱码,被这个坑困扰了大半天... axios post请求: download(index,row){ var ts = ...
- js之Ajax下载文件
传统上,客户端将依靠浏览器来处理从服务器下载文件.然而,这种方法需要打开一个新的浏览器窗口,iframe或任何其他类型的不友好和黑客行为.为下载请求添加额外的头信息也很困难.更好的解决方案是使用HTM ...
随机推荐
- sqlserver 安全
1.将数据库的用户名和密码加密保存,使用加密传输.2.将数据库里面的用户除了这个用户所有的用户都禁用,把该用户的密码改的很复杂,很难破解那种3.设置数据库的可连接方式(所有的方式的设置).4.删除数据 ...
- 《C++ Primer Plus》第5章 循环和关系表达式 学习笔记
C++提供了3种循环: for 循环. while 循环 和 do while 循环 .如果循环测试条件为 true 或非零,则循环将重复执行一组指令: 如果测试条件为 false 或 0 , 则结束 ...
- Eclipse启动Server报错:Could not publish to the server. java.lang.NullPointerException
转载自:http://m.blog.csdn.net/article/details?id=49862243 错误信息: publishing to tomcat v8.0 server at loc ...
- kafka 相关配置
kafka主要配置包括三类:broker configuration,producer configuration and consumer configuration. Broker Config ...
- 将工程导入到SVN仓库
1.在桌面右键点开Tortoise客户端 2.选择仓库 3.在仓库的trunk目录下为新工程创建文件夹
- Zabbix分布式监控
上一篇:Zabbix的API的使用 zabbix分布式监控 新建一台主机 安装zabbix proxy和数据库 yum -y install mariadb-server zabbix-proxy-m ...
- poj1742 Coins【多重背包】【贪心】
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions:43969 Accepted: 14873 Descriptio ...
- SignalR循序渐进(二)泛型Hub
接上一篇,文章末尾抛出了2个问题: 能不能让客户端声明一个强类型的方法列表呢?这样首先不容易写错. 同样的,能不能让服务端声明一个强类型的方法列表给客户端调用呢? 如果要让客户端的方法以强类型出现在服 ...
- OSGI框架—HelloWorld小实例
OSGi(Open Service Gateway Initiative)技术是Java动态化模块化系统的一系列规范.OSGi一方面指维护OSGi规范的OSGI官方联盟,另一方面指的是该组织维护的基于 ...
- golang函数学习笔记
golang函数特点: a.不支持重载,一个包不能有两个名字一样的函数 b.函数是一等公民,函数也是一种类型,一个函数可以赋值给变量 c.匿名函数 d.多返回值 例子1 func add(a, b ...