通过 VueJS 3 中的 Axios 从 Laravel API 下载 pdf
P粉993712159
P粉993712159 2023-11-16 12:48:50
0
2
648

你好,我有一个 VUEJS 3 的前端和 Laravel 8 的后端。我会下载保存在 public/pdf/temp/file.pdf 中的 pdf

现在我从 VUEJS 拨打电话:

axios.post('/api/'+ this.url_access +'/rebuild', formData, { headers: {
                'Content-Type': 'multipart/form-data',
                'responseType': 'blob'
            }})
            .then(response=>{
                if(response.status == 200){
                    const url = window.URL.createObjectURL(new Blob([response.data]));
                    const link = document.createElement('a');
                    link.href = url;
                    link.setAttribute('download', 'test.pdf');
                    document.body.appendChild(link);
                    link.click();
                }
            })
            .catch(error=>{
                console.log(error);
            })

在后端我有一个返回pdf文件的函数:

try{
   $headers = [
       'Content-Type' => 'application/pdf',
   ];
   return response()->download($file_path, $workspace['name'] . '_' .date("Ymd").'.pdf', $headers)->deleteFileAfterSend(true);
}catch(Exception $e){
   return $e->getMessage();
}

但是我下载了空白内容的pdf。

有人对这个问题有任何想法吗?

P粉993712159
P粉993712159

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!