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. @Override错误

    导入一个项目,项目所有类报  @Override 有错误,去掉就不报错了,原因?在 Java Compiler 将 Enable project specific setting 选中  然后再选择1 ...

  2. iOS json解析中包含“\n”等解析出错

    文题算是解决了,把特殊字符替换一下:-(NSString *)JSONString:(NSString *)aString {    NSMutableString *s = [NSMutableSt ...

  3. Spring 之注解事务 @Transactional(转载)

    Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 事务传播行为类型 事务传播行为类型 说明 P ...

  4. LeetCode-Lowest Common Ancestor of a Binary Tre

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. 170317、到底什么时候该使用MQ?

    一.缘起 一切脱离业务的架构设计与新技术引入都是耍流氓. 引入一个技术之前,首先应该解答的问题是,这个技术解决什么问题. 就像微服务分层架构之前,应该首先回答,为什么要引入微服务,微服务究竟解决什么问 ...

  6. 进击的RecyclerView入门一(简单上手)

    虽然RecyclerView面世有一段时间了,但由于它的学习成本相对较高,很多码友只是粗略的认识了一下而没有细致的品味RecyclerView的真谛. 那么从现在开始我将带你装逼带你飞,一起领略Goo ...

  7. Java中的常用方法

    Java中的常用方法 第一章 字符串 1.获取字符串的长度:length() 2.判断字符串的前缀或后缀与已知字符串是否相同    前缀 startsWith(String s).后缀 endsWit ...

  8. 爬虫用到的库Beautiful Soup

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时 ...

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

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

  10. 阿里云OSS分片上传DEMO

    分片传输规则 1.不能超过10000片,2.每片必须大于100KB using System; using System.Collections.Generic; using System.Compo ...