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)
          }
        })
      },
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章