vue使用js-file-down进行文件下载
本文共 3,112 字,预计阅读时间 10 分钟
1)安装js-file-download
npm i -S js-file-download
2)编写下载的页面
<template> <div> <el-button @click="download">下载</el-button> </div> </template> <script> import fileDownload from 'js-file-download' import request from '@/request' export default { methods: { download() { request.postDownload('file/download').then(res => { const disposition = res.headers["content-disposition"] //获取文件名 let fileName = disposition.substring(disposition.indexOf("filename=") + 9); if (window.navigator.userAgent.indexOf("Firefox") > -1) { fileName = fileName.substring(fileName.indexOf("-8?B?") + 5, fileName.indexOf("?=")) fileName = base64Util.decode(fileName); } else { fileName = decodeURIComponent(fileName); } fileDownload(res.data, fileName) }).catch(err => { console.log(err) }) } }, } </script> <style scoped> </style>
这里网络请求使用的axios,对其进行了封装。下载的方法很简单,主要是获取响应头中的文件名
3)编写下载的工具类
package com.zys.demo.util; import lombok.extern.slf4j.Slf4j; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; @Slf4j public class FileUtil { /** * 通过流下载文件 * * @param fileName 文件名 * @param response * @throws IOException */ public static void downloadFile(String filePath, String fileName, HttpServletResponse response) throws IOException { File file = new File(filePath + File.separator + fileName); InputStream in = new FileInputStream(file); //允许暴露content-disposition,默认不暴露 response.setHeader("Access-Control-Expose-Headers", "content-disposition"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/octet-stream"); response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); int len = 0; byte bytes[] = new byte[1024]; OutputStream out = response.getOutputStream(); while ((len = in.read(bytes)) > 0) { out.write(bytes, 0, len); } in.close(); out.close(); } }
需要注意的是必须设置响应头的Access-Control-Expose-Headers,也就是要暴露的信息,否则在前端无法获取到文件名。
4)编写下载的接口
package com.zys.demo.controller; import com.zys.demo.util.FileUtil; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @RestController @RequestMapping("/api/file") public class FileController { @PostMapping("/download") public void downloadFile(HttpServletResponse response) throws IOException { FileUtil.downloadFile("D://temp", "test.txt", response); } }
启动后,点击下载按钮即可下载。文件需事先存在于对应位置。
就是这么简单,你学废了吗?感觉有用的话,给笔者点个赞吧 !
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!