记一次处理后端返回的二进制流

63 阅读1分钟

首先发请求时应该携带responseType:blob

const downLoad = (res,fileName)=>{
  let url = window.URL.createObjectURL(
     new Blob([res],{type:'MIME类型'})
  )
  const a = document.createElement('a')
  a.style.display = 'none'
  a.href = url
  a.setAttribute('downLoad',fileName+'文件后缀名')
  document.body.appendChild(a)
  a.click()
  url = URL.revokeObjectURL(a.href)
  document.body.removeChild(a)
}

MIME类型参考这里