• 前端axios下载导出文件工具封装


    使用示例:

    import { fileDownload } from '@/utils/fileDownload'
    
    fileDownload({ url: process.env.VUE_APP_BASE_URL + `/statistic/pageList/export`, method: 'post', data: data })
    
    • 1
    • 2
    • 3

    工具类:

    import store from '../store/index'
    import {
        getAccessToken
    } from '@/utils'
    import axios from 'axios'
    const token = store.getters.token || getAccessToken()
    // 单个文件下载
    export const fileDownload = (obj) => {
        let axiosParams = {
            method: obj.method,
            url: process.env.VUE_APP_apipath + obj.url,
            responseType: 'blob',
            headers: {
                'Authorization': `Bearer ${token}`
            }
        }
        if (obj.method && 'POST' === obj.method.toUpperCase()) {
            axiosParams.data = obj.data ? obj.data : {}
        }
        if (obj.method && 'GET' === obj.method.toUpperCase()) {
            axiosParams.params = obj.params ? obj.params : {}
        }
        return axios(axiosParams).then(res => {
            if (res && res.data) {
                const url = window.URL.createObjectURL(
                    new Blob([res.data], {
                        type: 'application/octet-stream'
                    })
                )
                if (obj.noLink) {
                    return url
                }
                const link = document.createElement('a')
                link.style.display = 'none'
                link.href = url
                if (res.headers['content-disposition']) {
                    console.log(decodeURI(
                        res.headers['content-disposition'].slice(
                            res.headers['content-disposition'].indexOf('=') + 1,
                            res.headers['content-disposition'].indexOf('.')
                        )
                    ))
                    link.download =
                        decodeURI(
                            res.headers['content-disposition'].slice(
                                res.headers['content-disposition'].indexOf('=') + 1,
                                res.headers['content-disposition'].indexOf('.')
                            )
                        ) +
                        res.headers['content-disposition'].slice(
                            res.headers['content-disposition'].indexOf('.')
                        )
                } else {
                    if (obj.fileName) {
                        link.download = obj.fileName
                    }
                }
    
                document.body.appendChild(link)
                link.click()
                window.URL.revokeObjectURL(url)
            }
        })
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    后端返回设置:

    /**
     * excel内容类型
     */
    private static final String CONTENT_TYPE_EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
    /**
     * 编码
     */
    private static final String ENCODE = "UTF-8";
    
    /**
     * Content-disposition
     */
    private static final String CONTENT_DISPOSITION = "Content-disposition";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    response.setContentType(CONTENT_TYPE_EXCEL);
    response.setCharacterEncoding(ENCODE);
    // 防止文件名的中文乱码
    fileName = URLEncoder.encode(fileName, ENCODE).replaceAll("\\+", "%20");
    response.setHeader(CONTENT_DISPOSITION, "attachment;filename=" + fileName + ".xlsx");
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    Prometheus详解(十)——Prometheus容器监控
    JAVA安全之Shrio550-721漏洞原理及复现
    大数据治理的核心是什么
    国内家具行业数据浅析
    23062QTday3
    iOS ------ autoreleasePool
    对象与成员函数指针 function+bind
    Linux-实操篇8-shell脚本编写
    Linux内核链表(list)移植到任意平台
    【MySQL基础 | 中秋特辑】多表查询详细总结
  • 原文地址:https://blog.csdn.net/weixin_46099269/article/details/133795353