get 加 header 下载文件 函数,虽然最后没用。

export const apiDown = (url, data = {}) => {
  let data2 = secretFilter(data)
  axiosDown({
    url,
    params: data2,
    method: 'get',
    responseType: 'blob',
    headers: {
      'moudleId': 'rim',
      // 'Content-Type': _contentType,
      token: getToken() ? getToken() : ''
    }
  }).then(response => {
    console.info('apiGet res', response)
    if (response.data.type == 'application/octet-stream') {
      let filename = ''
      if (response.config.params.fileName) {
        filename = decrypt2(response.config.params.fileName)
      }
      if (response.config.params.id) {
        console.info('response.config.params.id', response.config.params.id)
        filename = base64ToStr(response.config.params.id).split('_')[1]
      }
      const blob = new Blob([response.data])
      var downloadElement = document.createElement('a')
      var href = window.URL.createObjectURL(blob)
      downloadElement.href = href
      downloadElement.download = filename
      document.body.appendChild(downloadElement)
      downloadElement.click()
      document.body.removeChild(downloadElement)
      window.URL.revokeObjectURL(href)
    }
  })
}
posted @ 2021-10-19 14:46  彭成刚  阅读(105)  评论(0编辑  收藏  举报