首页 > 其他分享 >axios文件下载!!!!

axios文件下载!!!!

时间:2023-03-23 13:46:07浏览次数:50  
标签:文件 axios res excel value UserExcel new import 下载

前端

download(){
                debugger;
                this.loading = true;
                axios.post('http://localhost:8081/brand_case/dao.do?method=export',this.queryParams,{
                    responseType: 'blob'
                }).then( res=>{
                    console.log('res', res)
                    // var data = res.data.data;
                    // 截取文件名,这里是后端返回了文件名+后缀,如果没有可以自己拼接
                    // 切割出文件名
                    const fileNameEncode = res.headers['content-disposition'].split('filename=')[1]
                    /// 解码
                    const fileName = decodeURIComponent(fileNameEncode)
                    console.log('fileName', fileName)
                        // 设置type类型
                        const blob = new Blob([res.data], {
                            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; application/octet-stream'
                        })
                        const fileUrl = window.URL.createObjectURL(blob)
                        let a = document.createElement("a");
                        a.href = fileUrl
                        console.log('url', fileUrl)
                        a.setAttribute('download', fileName)
                        a.style.display = 'none'
                        a.click()
                        a.remove()
                        this.loading = false;
                    }
                )
            }
        }

后端---模板

package com.itheima.pojo;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@HeadRowHeight(value = 30)
@ContentRowHeight(value = 25)
@ColumnWidth(value = 30)
public class UserExcel {
    @ExcelProperty(value = "用户编号")
    private Integer userId;
    @ExcelProperty(value = "姓名")
    private String username;
    @ExcelProperty(value = "性别")
    private String gender;
    @ExcelProperty(value = "工资")
    private Double salary;
    @ExcelProperty(value = "入职时间")
    private Date hireDate;
}

controller

 // 设置响应头
        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");
        // 设置防止中文名乱码
        String filename = URLEncoder.encode("员工信息", "utf-8");
        // 文件下载方式(附件下载还是在当前浏览器打开)
        resp.setHeader("Content-disposition", "attachment;filename=" +
                filename + ".xlsx");
        // 构建写入到excel文件的数据
        List<UserExcel> userExcels = new ArrayList<>();
        UserExcel userExce1 = new UserExcel(1001, "张三", "男", 1333.33,
                new Date());
        UserExcel userExce2 = new UserExcel(1002, "李四", "男", 1356.83,
                new Date());
        UserExcel userExce3 = new UserExcel(1003, "王五", "男", 1883.66,
                new Date());
        UserExcel userExce4 = new UserExcel(1004, "赵六", "男", 1393.39,
                new Date());
        userExcels.add(userExce1);
        userExcels.add(userExce2);
        userExcels.add(userExce3);
        userExcels.add(userExce4);
        // 写入数据到excel
        EasyExcel.write(resp.getOutputStream(), SysOperLog.class)
                .sheet("用户信息")
                .doWrite(logQuery);

标签:文件,axios,res,excel,value,UserExcel,new,import,下载
From: https://www.cnblogs.com/sy2022/p/17247162.html

相关文章

  • linux 文件解压
    tar命令一必选参数-c表示创建文件-create-x表示解压缩文件-t 表示查看压缩包里的文件-r 追加压缩文件-u更新压缩包中的文件-f 指定文档名后面不能接参......
  • 文件操作
    #include<iostream>#include<algorithm>#include<cstdio>#include<queue>#include<cstring>#include<cstdlib>#include<vector>#include<cmath>#include<......
  • 文件管理
    磁盘相关来自计组磁盘存储器的组成磁盘驱动器:磁头和盘片。温彻斯特盘是一种可移动磁头固定盘片的磁盘。磁盘控制器:与主机的接口。主流标准有IDE、SCSI、SATA等。......
  • windows下编写的脚本文件,放到Linux中无法执行解决方法
    在windows下编写的脚本文件,放到Linux中执行报错:shstartup.sh-mstandalone:commandnotfound:comma......
  • PHP 批量生成 WORD2007 文件
    由于MSOFFICE07中新的文件格式启用了zip+xml的方式,这也就意味着可以用PHP方便地来实现WORD中数据的自动生成的功能。一个插件:openxmlphpword可以满足你的需求。 它只有......
  • Java初学者推荐学习书籍free下载
    场景Java是由SunMicrosystems公司于1995年5月推出的高级程序设计语言。Java可运行于多个平台,如Windows,MacOS,及其他多种UNIX版本的系统。Java分为三个体系:JavaSE(J2SE)(Jav......
  • 349套HTML5+CSS3各行各业网站模板免费下载
    场景349套HTML5+CSS3各行各业网站模板。Web前端开发是从网页制作演变而来的,名称上有很明显的时代特征。在互联网的演化进程中,网页制作是Web1.0时代产物,那时网站的主要内容是......
  • 2019云栖大会精品资料免费下载
    场景杭州云栖大会前身为阿里云开发者大会,是中国最早的开发者创新展示平台。过去10年间,伴随互联网和云计算的蓬勃发展,大会规模逐年扩大。2018年云栖大会吸引了来自81个国家及......
  • 专业、免费、超好用的软件测试题库《柠檬班》使用教程(一)--题库下载安装/使用
    关于我们的app/小程序免费使用,8000+题目,全部免费目前app已上架终端平台:华为、小米、oppo、vivo、苹果安卓平台:官方应用市场/商店苹果平台:appstore当前版本:1.2......
  • a标签实现下载远程文件
    通过a标签可以下载文件(图片、音频、视频等)constlink=document.createElement('a');link.href=fileurl;link.download=filename;link.click();远程文件地址,直......