简体   繁体   English

下载 pdf 文件 (blob) 时出现 javascript 错误

[英]javascript error when downloading pdf file (blob)

I am using this approach for downloading a pdf file from server (laravel 8 (api sanctum) + vue 3)我正在使用这种方法从服务器(laravel 8(api sanctum)+ vue 3)下载一个 pdf 文件

In the vue component I have this function that downloads the file在 vue 组件中,我有这个 function 下载文件

const onDownloadDocument = (id) => {           
   axios.post('/api/document/download', {id: id},{
     responseType: 'blob'
   }).then(response => {
     let filename = response.headers['content-disposition'].split('filename=')[1]               
     dLink.value.href = window.URL.createObjectURL(response.data)
     dLink.value.setAttribute('download',filename)
     dLink.value.click()
   }).catch(error => {
      console.log(error)
   })

where dLink is a link ref其中 dLink 是链接引用

const dLink = ref(null)

in template:在模板中:

<a ref="dLink"/>

It works this approach until today.... after I updated the project (composer update and npm update)直到今天它都采用这种方法....在我更新项目之后(作曲家更新和 npm 更新)

Now when click to download the file (call the onDownloadDocument function) I get an error:现在,当单击下载文件(调用 onDownloadDocument 函数)时,出现错误:

contract.js:1049 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'responseType')

Any idea why?知道为什么吗?

The api backend return the file blob api 后端返回文件 blob

return Storage::download($document->filename);

First you need to create a blob and put your response in it,首先,您需要创建一个 blob 并将您的响应放入其中,

And as I said in my comment you don't need to attach to a real anchor tag, you can just create an element, attach it to the body, simulate the click and remove it immediately正如我在评论中所说,您不需要附加到真正的锚标签,您只需创建一个元素,将其附加到正文,模拟点击并立即删除它

const blob = new Blob([response], {type: 'application/pdf'})
if (window.navigator['msSaveOrOpenBlob']) {
    window.navigator['msSaveBlob'](blob, filename)
}
else {
    const elem = window.document.createElement('a')
    elem.href = window.URL.createObjectURL(blob)
    elem.download = filename
    document.body.appendChild(elem)
    elem.click()
    document.body.removeChild(elem)
}

This worked for me, specify to axios that you are expecting a blog, and that's it:这对我有用,向 axios 指定您期待的博客,仅此而已:

axios({
  url: 'http://localhost:5000/endpoint?',
  method: 'GET',
  responseType: 'blob', // <= important
}).then((response) => {
  const url = window.URL.createObjectURL(new Blob([response.data]));
  const link = document.createElement('a');
  link.href = url;
  link.setAttribute('download', 'file.pdf');
  document.body.appendChild(link);
  link.click();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM