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

使用示例:

javascript 复制代码
import { fileDownload } from '@/utils/fileDownload'

fileDownload({ url: process.env.VUE_APP_BASE_URL + `/statistic/pageList/export`, method: 'post', data: data })

工具类:

javascript 复制代码
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)
        }
    })
}

后端返回设置:


相关推荐
KaMeidebaby22 分钟前
卡梅德生物技术快报|PD1 单克隆抗体定制配套 N 糖全谱质控开发
前端·人工智能·算法·数据挖掘·数据分析
nuIl1 小时前
实现一个 Coding Agent(3):工具调用
前端·agent·cursor
nuIl1 小时前
实现一个 Coding Agent(4):ReAct 循环
前端·agent·cursor
AAA大运重卡何师傅(专跑国道)1 小时前
【无标题】
开发语言·c#
nuIl1 小时前
实现一个 Coding Agent(1):一次 LLM 调用
前端·agent·cursor
nuIl1 小时前
实现一个 Coding Agent(2):让 LLM 流式响应
前端·agent·cursor
copyer_xyf1 小时前
Python 异常处理
前端·后端·python
sugar__salt2 小时前
从栈队列数据结构到JS原型面向对象全解
前端·javascript·数据结构
XBodhi.2 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
MageGojo2 小时前
随机文案模块怎么做?从接口封装到前端展示的完整实现思路
javascript·前端开发·api接口·后端开发·随机文案