强制浏览器从laravel中的外部服务器下载图像文件

我项目中的图像来自外部服务器;我想在单击下载按钮或标签时下载它们,但在另一页中打开它们。

                @foreach($items as $item)
                <div class="col-lg-2 col-md-4 col-sm-6 nopad image-gallery">
                    <a target="_blank" class="fancybox mediaReportItem persianNum downloadImg" rel="fancybox-button"
                       downloadpath="{{ $mediaAddress. '/' .$item->image->original_path }}" id="{{ $item->id }}"
                       href="{{ $mediaAddress. '/' .$item->image->original_path }}" title="{{ $item->title }}">
                        <img src="{{ $mediaAddress. '/' .$item->image->thumb_path }}" class="photoThumbnaill"
                             alt="{{ $item->title }}" download></a>
                </div>
            @endforeach

下载不起作用,因为图像在外部服务器上。

response()->download($file);
napkinnn 回答:强制浏览器从laravel中的外部服务器下载图像文件

由于您需要设置标头(因为a[download]仅适用于同源网址),因此需要代理文件,即创建返回文件内容的路由。

一个简单的实现:

return response(file_get_contents($url),200,[
    'Content-Disposition' => 'attachment; filename="filename.png"',]);
本文链接:https://www.f2er.com/2993296.html

大家都在问