Laravel从S3路径下载文件(无法在浏览器中打开) [英] Laravel download file from S3 route (not open in browser)

查看:260
本文介绍了Laravel从S3路径下载文件(无法在浏览器中打开)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下路线将从给定的URL加载文件,我需要这样做才能实际下载文件(mp4,jpg,pdf),而不是在内置查看器中的浏览器中打开文件.

I have the following route that will load a file from the given URL, I need this to actually download the file (mp4, jpg, pdf) rather than open in the browsers in built viewer.

// Download from CDN Route
Route::get('cdn/{url}', function($url)
    {   
        return Redirect::away($url);    
    })->where('url', '(.*)');

所有文件都存储在外部,因此Resource::download()显然无法正常工作.

All files are stored externally so apparently Resource::download() wouldn't actually work.

我所能使用的就是Amazon URL:https://mybucket.s3.amazonaws.com/folder/filename.pdf

All I have available to me is the Amazon URL: https://mybucket.s3.amazonaws.com/folder/filename.pdf

关于如何强制浏览器从S3下载文件的任何建议?

Any suggestions on how to force the browser to download the file from S3?

推荐答案

我们如何从另一台服务器下载带有外部链接的视频.不是s3.这是一台普通的服务器,文件在那里,但是不起作用.我已经写了这段代码

How can we download a video with an external link from another server. It's not s3. It's a normal server, files are there, but it doesn't work. I have written this code

                $filename = $video->video_id;
                $tempFile = tempnam(sys_get_temp_dir(), $filename);
//              dd($tempFile);

                copy($video->path, $tempFile);
                header("Content-Disposition: attachment; filename = ".$filename);
                header("X-Accel-Redirect: ".$filename);

                return response()->download($tempFile, $filename);

这篇关于Laravel从S3路径下载文件(无法在浏览器中打开)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆