编程知识 cdmana.com

axios 上传/下载文件

下载文件

//下载文件请求
...then(response => {
    
    const url = URL.createObjectURL(response.data)
    const link = document.createElement('a') // 创建a标签
    link.href = url
    
    // 下载文件名默认会用响应头'content-disposition'中的filename属性值。其格式一般为:Content-Disposition: attachment; filename="filename.jpg",取值:response.headers["content-disposition"].split("filename=")[1]
    // 如果需要自定义文件名,则需要手动设置 link.download = decodeURIComponent("自定义名称.jpg")
    
    link.click()
    URL.revokeObjectURL(url) // 释放内存
})

将下载的文件再上传

//下载文件请求
...then(response => {
    
    //let blob = new Blob([response.data], {'type': 'application/octet-stream;charset=UTF-8'})
    // 上传文件
    let formData = new FormData()
    formData.append('file', response.data)
    axios({
    
        method: 'post',
        url: '/upload',
        headers: {
    
            'Content-Type': 'multipart/form-data'
        },
        data: formData
    }).then(res => {
    
        resolve(res)
    }).catch(err => {
    
        reject(err)
    })
})

版权声明
本文为[markix]所创,转载请带上原文链接,感谢
https://markix.blog.csdn.net/article/details/121585448

Scroll to Top