怎么在springboot中利用vue实现在页面中下载文件

  介绍

怎么在springboot中利用vue实现在页面中下载文件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1。前端代码:

& lt; template  v-slot:操作=皗},row “比;   ,,& lt; vxe-button ,类=癳l-icon-download", title=俺晒略亍?circle  @click=癲ownloadFile(行)“祝辞& lt;/vxe-button>   & lt;/template>   downloadFile(行),{=,window.location “http://localhost: 8001/文件/downloadFile ? taskId=? +, row.id;   }

2。后端代码:

package  com.gridknow.analyse.controller;         import  com.alibaba.fastjson.JSON;   import  com.gridknow.analyse.entity.DataInfo;   import  com.gridknow.analyse.service.FileService;   import  com.gridknow.analyse.utils.Download;   import  com.gridknow.analyse.utils.Result;   import  org.springframework.beans.factory.annotation.Autowired;   import  org.springframework.beans.factory.annotation.Value;   import  org.springframework.core.io.InputStreamResource;   import  org.springframework.http.HttpHeaders;   import  org.springframework.http.MediaType;   import  org.springframework.http.ResponseEntity;   import  org.springframework.stereotype.Controller;   import  org.springframework.web.bind.annotation。*;   import  org.springframework.web.multipart.MultipartFile;   import  org.springframework.web.multipart.MultipartHttpServletRequest;      import  javax.servlet.http.HttpServletResponse;   import  . io . *;   import 并不知道;   import  java.util.Map;/* *   ,* @ClassName  FileController   ,* @Description:待办事项   ,* @Author 管理员   ,* @Date  2020/8/20  14:02   ,* @Version  TODO   * */@ controller   @RequestMapping (“/file")   public  class  FileController  {      ,@ value (“$ {gridknow.mltc.imgurl}“)   ,private  String  imgUrl;      ,@ autowired   ,private  FileService  fileService;         ,@CrossOrigin=,@RequestMapping (value “/upload",, method =, RequestMethod.POST)   ,@ResponseBody   ,public  Result 上传(MultipartHttpServletRequest 请求),{   List<才能;MultipartFile>, multipartFiles =, request.getFiles (“file");   Map<才能,字符串,Object>, map =,(字符串,Map), JSON.parse (request.getParameter (“body"));   String 才能;companyId =, request.getParameter (“companyId");   String 才能;companyName =, request.getParameter (“companyName");   boolean 才能;bool =, fileService.uploadAndInsert (multipartFiles,地图,companyId,,公司名称);   if 才能;(bool), {   ,,return  new 结果(200);   ,,},{else    ,,return  new 结果(500);   ,,}   ,}      ,@GetMapping (“/downloadFile")   ,public  ResponseEntity downloadFile (@RequestParam (“taskId"), String  taskId,, HttpServletResponse 响应),{   DataInfo 才能;DataInfo =, fileService.queryTaskById (taskId);   if 才能;(dataInfo ==, null), {   ,,return 零;   ,,}   File 才能;File =, new 文件(dataInfo.getResponseUrl ());//,才能文件下载   if 才能;(file.isFile ()), {   ,,return  downloadFile (taskId);   ,,}//,才能文件夹压缩成zip下载   if 才能;(file.isDirectory ()), {   ,,String  parent =, file.getParent ();   ,,//,创建临时存放文件夹   ,,File  temDir =, new 文件(时间+ parent “/? +, taskId);   ,,if  (! temDir.exists ()), {   ,,,temDir.mkdirs ();   ,,}   ,,//,将需要下载的文件夹和内容拷贝到临时文件夹中   ,,try  {   ,,,Download.copyDir (dataInfo.getResponseUrl (),, parent  +,“/? +, taskId);   ,,},catch  (IOException  e), {   ,,,e.printStackTrace ();   ,,}//,,,设置头部格式   ,,response.setContentType(“应用程序/zip");   ,,response.setHeader (“Content-Disposition",,“附件;,文件名=? taskId +“.zip");//,,,调用工具类,下载压缩压缩包   ,,try  {   ,,,Download.toZip (temDir.getPath (),, response.getOutputStream(),,真的);   ,,},catch  (IOException  e), {   ,,,e.printStackTrace ();   ,,}//,,,删除临时文件夹和内容   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在springboot中利用vue实现在页面中下载文件

Copyright © 2020-2023 feiqueyun.cn. All Rights Reserved. 肥雀云_南京肥雀信息技术有限公司版权所有 南京肥雀信息技术有限公司 苏ICP备16063723号-5