当前位置:首页 > 开发 > 正文内容

axios下载文件

hackcode2023年09月07日 14:42开发454
axios.get(url, {
        params: {
          
        },
        headers: {
          Authorization: 
        },
        responseType: 'blob' // 切记类型 blob
      }).then((res) => {
        console.log(res)
        const blob = new Blob([res.data])
        const url = window.URL.createObjectURL(blob) // 创建 url 并指向 blob
        const a = document.createElement('a')
        a.href = url
        a.setAttribute('download', 'excel.xls')
        document.body.appendChild(a)
        a.click()
        window.URL.revokeObjectURL(url) // 释放该 url
      }).catch((err) => {
        console.log(err)
      })