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)
})
發佈了118 篇原創文章 · 獲贊 18 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章