根据url 下载文件

// 区分ie浏览器和chrome浏览器实现修改文件名
function downloadFile(url, fileName) {
  axios({
    method: 'get',
    url: url,
    // data:  encryptList.Encrypt(JSON.stringify(params)),
    // data: params,
    responseType: 'blob',
    // headers: {
    //   'token': tool.getToken(),
    //   appId: 36,
    //   'Content-Type': 'application/x-www-form-urlencoded'
    // }
  }).then(res => {
    const link = document.createElement('a')
    const blob = new Blob([res.data], {
      type: 'application/vnd.ms-excel'
    })
    link.style.display = 'none'
    link.href = URL.createObjectURL(blob)
    link.setAttribute('download', `${fileName}`)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link);
  }).catch(err => {
    console.log('导出报错', err);
  });
}

 

posted @ 2022-01-05 16:07  张小中  阅读(298)  评论(0编辑  收藏  举报