vue下载文件
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 = 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; }