vue 下载文件

import axios from 'axios'

let filename = xxx

axios({
    url: '/api/xxx/',
    method: 'post',
    responseType: 'blob',
    params: {
        xxx: xxxx
        ...
    }
}).then(res => {
    let blob = new Blob([res.data])
    if ('download' in document.createElement('a')) {
        let link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = filename
        link.click()
        window.URL.revokeObjectURL(link.href)
    } else {
        navigator.msSaveBlob(blob, filename)
    }
  }).catch(error => {
    console.log('error: ', error)
})

你可能感兴趣的:(前端,vue)