利用a标签下载文件

利用a标签的download属性下载文件

axios({
method: 'get', // 此处不一定只是get方法,也可以通过参数传递
url: url,
responseType: 'blob', // 此处重点:标明后端返回数据类型为流
}).then(res => {
let blob = new Blob([res.data], {
// 下载的文件类型(此处可更改:具体取值参考以下链接地址)
type: "application/vnd.ms-excel"
});
downloadFile(blob,'xxx','zip')
}).catch(error => {
console.log('下载文件失败')
});

function downloadFile(obj, name, suffix) {
  const url = window.URL.createObjectURL(obj)
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  const fileName = parseTime(new Date()) + '-' + name + '.' + suffix
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

  

 

posted @ 2021-08-25 10:23  webHYT  阅读(337)  评论(0)    收藏  举报