前端基于axios请求下载文件(后端返回Blob文件流)

前端小白第一篇csdn文章就当自己记录学习啦!

我自己遇到的情况写在前面防止有人和我不一样,浪费时间浏览;

调用下载接口后端给我返回的数据格式(即文件流格式)如下:

1.按钮定义点击事件

2.调用后端接口

在使用 axios 请求下载文件 api 接口时,注意区分不同请求方法的使用,语法如下:

// axios设置reponseType的方式应该类似下面
const url = '/info/download'
 
// get、delete、head 等请求
axios.get(url, {params: {}, responseType: 'blob'})
    .then((res) => {})
    .catch((err) => {})
 
// post、put、patch 等请求
axios.post(url, {...params}, {responseType: 'blob'})
    .then((res) => {})
    .catch((err) => {})

3.具体实现代码如下(可直接复制粘贴):

注:service是封装了axios

 

 download() {

      service({

        method: "get",

        url: `后端提供的接口`,

        responseType: "blob",

      }).then((res) => {

        console.log(res);

        console.log(res.data.type);

        let blob = new Blob([res.data], { type: res.data.type });

        let url = window.URL.createObjectURL(blob);

        console.log(url);

        let link = document.createElement("a");

        link.style.display = "none";

        link.href = url;

        link.setAttribute("download", "文件名+后缀");//文件名后缀记得添加,我写的zip

        document.body.appendChild(link);

        link.click();

        document.body.removeChild(link);//下载完成移除元素

        window.URL.revokeObjectURL(url);//释放掉blob对象

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端下载 Excel 文件后端需要返回文件,可以按照以下步骤进行处理: 1. 后端根据前端请求参数,生成 Excel 文件,并将文件返回前端。 2. 前端通过 AJAX 发送请求请求后端生成 Excel 文件的接口。 3. 后端在接收到请求后,使用相关的库(如 Apache POI)生成 Excel 文件,并将文件返回前端。 4. 前端在接收到后端返回文件后,使用 Blob 对象创建一个 URL,并将该 URL 赋给一个 a 标签的 href 属性。 5. 前端再使用 JavaScript 触发该 a 标签的 click 事件,即可下载 Excel 文件。 以下是一个示例的后端代码,使用了 Spring Boot 和 Apache POI 库: ```java @GetMapping("/downloadExcel") public ResponseEntity<byte[]> downloadExcel() throws IOException { // 创建 Excel 工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 创建表头 Row header = sheet.createRow(0); header.createCell(0).setCellValue("姓名"); header.createCell(1).setCellValue("年龄"); // 创建数据行 Row dataRow = sheet.createRow(1); dataRow.createCell(0).setCellValue("张三"); dataRow.createCell(1).setCellValue(20); // 将 Excel 文件写入字节数组输出 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); // 设置响应头 HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename=test.xlsx"); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 返回响应实体 return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK); } ``` 在前端中,可以使用以下代码实现下载: ```javascript axios.get('/downloadExcel', { responseType: 'blob' }).then(res => { const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = 'test.xlsx' a.click() URL.revokeObjectURL(url) }) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值