vue+springboot通过post请求实现文件下载
前端vue实现
let param = { 'fileName': 'fileName' } this.download(this.BaseURL, '/downloadFile', param, (response) => {}, 5 * 1000 * 1000) // 封装 download (baseURL, url, data, callback, timeout) { return axios({ baseURL: baseURL, method: 'post', url, data: data, responseType: 'blob',//设置responseType为blob timeout: timeout }).then( (response) => { if (response.headers['content-type'] === 'APPLICATION/OCTET-STREAM') { let filename = 'result.zip' //response.data为下载的文件数据 let url = window.URL.createObjectURL(new Blob([response.data])) let link = document.createElement('a') link.style.display = 'none' link.href = url link.setAttribute('download', filename) document.body.appendChild(link) link.click() } } ) }
后端下载方法
@RequestMapping(value = "downloadFile", method = RequestMethod.POST) public void downloadFile(@RequestBody DownloadFileDTO downloadFileDTO, HttpServletResponse response) throws IOException { response.setContentType("APPLICATION/OCTET-STREAM"); ZipOutputStream out = new ZipOutputStream(response.getOutputStream()); // 获取文件存储路径(绝对路径) try { String fileName = downloadFileDTO.getFileName(); String[] filePaths = fileName.split(","); for (String path : filePaths) { File file = new File(path); ZipUtils.doCompress(file.getCanonicalPath(), out); //打包压缩 response.flushBuffer(); } } catch (Exception e) { e.printStackTrace(); } finally { out.close(); } }
做你说过的 说你能做的;

浙公网安备 33010602011771号