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

强制浏览器从laravel中的外部服务器下载图像文件,laravel,laravel-5,external,downloadfile,Laravel,Laravel 5,External,Downloadfile,我的项目中的图像来自外部服务器;我想在点击下载按钮或标签时下载它们,但在另一个页面中打开它们 @foreach($items as $item) <div class="col-lg-2 col-md-4 col-sm-6 nopad image-gallery"> <a target="_blank" class="fancybox mediaReportItem pers

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

                @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

由于您需要设置标题(因为[下载]仅适用于同一源URL),因此您需要代理文件,即创建返回文件内容的路由

一个简单的实现:

return response(file_get_contents($url), 200, [
    'Content-Disposition' => 'attachment; filename="filename.png"',
]);

嘿,你能分享$item数组或obj吗?不管你传递什么,只要两次迭代,我就可以测试代码了
return response(file_get_contents($url), 200, [
    'Content-Disposition' => 'attachment; filename="filename.png"',
]);