vue下载文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue下载文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3418字,纯文字阅读大概需要5分钟。
内容图文
![vue下载文件](/upload/InfoBanner/zyjiaocheng/1091/75c7cef8908d498ab2f1c40d12289103.jpg)
import fileDownload from ‘js-file-download‘
let params = { "token" : "123456", "id" : "12345678901234567", "filename":"123.rar", "pckid":"5", "dty":"tbox", "cml":"300", "version":"102", }; 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: 2000}); });
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 = newbyte[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(); } } } } } returnnull; }
原文:https://www.cnblogs.com/yaowen/p/9060268.html
内容总结
以上是互联网集市为您收集整理的vue下载文件全部内容,希望文章能够帮你解决vue下载文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。