使用Vue.js和Laravel下载图像的方法
P粉513318114
P粉513318114 2024-03-19 18:39:21
0
1
397

我点击此按钮有一个超链接按钮,我想从数据库获取图像并使用 laravel 和 vue js 将其下载到用户端。下面是我的脚本文件代码

getImage: function() {
            axios.get('/getImage/' + this.form.cashout_id )
            .then(function (r) 
                {
                const url = window.URL.createObjectURL(new Blob([r.data]));
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', 'file.'+r.headers.ext); //or any other extension
                document.body.appendChild(link);
                link.click();

                //hide loader
                i.loader = false
            })
            .catch(function (error) {
                        alert('Error');
            });
        },

现在这是我正在获取图像的控制器代码。

public function getimage($id)
   { 
       $cashout = CashOutDetail::findorfail($id);
       $storage_date = Carbon::parse($cashout['recorded_date']);

       return response()->download(
           storage_path('app/cashoutdetails/'. $storage_date->year .'/' . $storage_date->format('M') . '/'.  $cashout->bank_receipt),
           'filename.jpg',
           ['Content-Type' => 'image/jpg']
       );
   }

问题是我的图像正在被获取并显示在控制台窗口中,但无法下载。有人可以帮忙吗?

P粉513318114
P粉513318114

全部回复(1)
P粉391677921

你应该尝试:

axios({
    method: 'GET',
    url: '/getImage/123.jpg',
    responseType: 'blob', //  {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', '123.jpg');
    document.body.appendChild(link);
    link.click();
  });
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!