Laravel 在公共文件夹中下载 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19012832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:28:43  来源:igfitidea点击:

Laravel download pdf in public folder

laravel

提问by Ben

I have a a pdf in public/downloads. I want to just link to it and have it download or open in browser. I tried hitting http://localhost:8000/downloads/brochure.pdfin the browser but I just get a white screen with no errors. In Chrome DevTools > Network, it says the request was canceled. I'm inserting the URL via javascript so I can't use URL::to or link_to() like other answers on here have suggested.

我在公共/下载中有 pdf。我只想链接到它并让它在浏览器中下载或打开。我尝试http://localhost:8000/downloads/brochure.pdf在浏览器中点击,但我只是得到一个没有错误的白屏。在 Chrome DevTools > Network 中,它说请求已取消。我正在通过 javascript 插入 URL,所以我不能像这里的其他答案建议的那样使用 URL::to 或 link_to()。

Note: When I link to a css file in the same fashion as the brochure.pdf, the css appears in the browser.

注意:当我以与小册子.pdf 相同的方式链接到 css 文件时,该 css 会出现在浏览器中。

回答by star18bit

Path to file can be achieved with the following code:

文件路径可以通过以下代码实现:

public function getDownload(){

        $file = public_path()."/downloads/info.pdf";
        $headers = array('Content-Type: application/pdf',);
        return Response::download($file, 'info.pdf',$headers);
    }

function will download file from : 'project/public/downloads' folder.

函数将从:'project/public/downloads' 文件夹下载文件。

(don't forget to set-up routes and controller by yourself)

(不要忘记自己设置路由和控制器)

回答by SamSquanch

Route::get('download',function(){
    return Response::download($path_to_file,'name_of_file');
});