vue下载pdf文件

fetch("PDF的地址", {
  method: 'get',
  responseType: 'arraybuffer',
}).then(res => {
  if (res.status !== 200) {
    return res.json()
  }
  return res.arrayBuffer()
}).then(blobRes => {
  // 生成 Blob 对象,设置 type 等信息
  const e = new Blob([blobRes], {
    type: 'application/octet-stream',
    'Content-Disposition':'attachment'
  })
  // 将 Blob 对象转为 url
  const link = window.URL.createObjectURL(e)
  let a = document.createElement('a');
  a.href = link;
  a.download = "名称";
  a.click();
}).catch(err => {
  console.error(err)
})
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐