vue下载文件常用的几种方式

本文介绍Vue中实现文件下载的多种方法,包括直接打开、使用axios请求等,详细讲解了如何通过自定义方法处理文件下载,并提供了具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue下载文件常用的几种方式

一、直接打开

直接打开是指我们直接使用window.open(URL)的方法
优点:简单操作
缺点:没办法携带token

二、我们可以自己封装一个方法,比如如下:

import axios from "axios"
import * as auth from '@/utils/auth.js'

let ajax = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 100000
});

ajax.interceptors.request.use(config => {
        config.headers = {
            Authorization: auth.getToken(),
            // OrgId: auth.getUser().orgId,
            // UserId: auth.getUser().id,
        }
        return config
    },
    err => {
        return Promise.reject(err)
    })

let downloadFile = async (url, formData, options) => {
    await ajax.post(url, formData, {responseType: 'arraybuffer'}).then(resp => download(resp, options))
}

let getFile = async (url, options) => {
    await ajax.get(url, {responseType: 'blob'}).then(resp => download(resp, options))
}

let download = (resp, options) => {
    let blob = new Blob([resp.data], {type: options.fileType ? options.fileType : 'application/octet-binary'})
    //创建下载的链接
    let href = window.URL.createObjectURL(blob)
    downloadBlob(href, options.fileName)
}

let downloadBlob = (blobUrl, fileName, revokeObjectURL) => {
    let downloadElement = document.createElement('a')
    downloadElement.href = blobUrl
    //下载后文件名
    downloadElement.download = fileName
    document.body.appendChild(downloadElement)
    //点击下载
    downloadElement.click()
    //下载完成移除元素
    document.body.removeChild(downloadElement)
    if (revokeObjectURL == null || revokeObjectURL) {
        //释放掉blob对象
        window.URL.revokeObjectURL(blobUrl)
    }
}

let getDownloadFileUrl = async (url, fileType) => {
    let blob
    await ajax.get(url, {responseType: 'blob'}).then(resp => {
        blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
    })
    return window.URL.createObjectURL(blob);
}

let getDownloadFileUrlByPost = async (url, data, fileType) => {
    let blob
    await ajax.post(url, data, {responseType: 'blob'}).then(resp => {
        blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
    })
    return window.URL.createObjectURL(blob);
}

let getDownloadFileBlob = async (url, fileType) => {
    let blob
    await ajax.get(url, {responseType: 'blob'}).then(resp => {
        blob = new Blob([resp.data], {type: fileType ? fileType : 'application/octet-binary'});
    })
    return blob;
}

export default {
    ajax,
    downloadFile,
    getFile,
    getDownloadFileUrl,
    getDownloadFileUrlByPost,
    getDownloadFileBlob,
    downloadBlob
}

然后在我们调用的那个页面中直接引入使用就好啦

//先引用
import ajax from '../../utils/ajax.js'
//使用
ajax.downloadFile('URL',null,{下载的文件名称})

这样看是不是就挺容易的
希望能帮助到你

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有思想的前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值