axios获取后台返回的二进制流并下载下来

 down (val, pid) {
        this.fileName=val//自己定义的文件名称
        axios({
          method: 'post',
          url: myfun()+'/router/downLoad?routerId=' + val + '&routerPid=' + pid + '',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            Authentication: store.state.account.token//我这块需要传递token
          },
          responseType: 'arraybuffer',//arraybuffer/blob//加上格式,二进制
        }).then(res => {
          console.log(res)
          console.log(res.data)
          const blob = new Blob([res.data], {type: 'application/zip;charset=utf-8'});
          const fileName = this.fileName+'_config.ini'//下载的文件名称及其后缀,后缀要和后台保持的一致
          if ('download' in document.createElement('a')) { // 非IE下载
            const elink = document.createElement('a')
            elink.download = fileName
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
          } else { // IE10+下载
            navigator.msSaveBlob(blob, fileName)
          }
        })
      },
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章