Laravel 5 Force 下载 pdf 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28465434/
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 10:54:21  来源:igfitidea点击:

Laravel 5 Force Download a pdf File

phphttppdflaravellaravel-5

提问by Ahmed Badawy

Edited
i want to Force Download a pdf File in laravel 5. in laravel 4 i used the following code:

编辑
我想在laravel 5中强制下载pdf文件。在laravel 4中我使用了以下代码:

        $file= public_path(). "/site-docs/cv.pdf";
    $headers = array(
        'Content-Type: application/pdf',
        'Content-Disposition:attachment; filename="cv.pdf"',
        'Content-Transfer-Encoding:binary',
        'Content-Length:'.filesize($file),
    );
    return \Response::download($file,"Ahmed Badawy - CV.pdf", $headers);

but in laravel 5 that doesn't work.it comes out with this error:

但是在 Laravel 5 中不起作用。它出现了这个错误:

Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)

also i tried this:

我也试过这个:

return response()->download($file);

same error apeared... what can i do to force a download in laravel 5.

同样的错误出现......我能做些什么来强制在laravel 5中下载。

I Solved it- Answer is:

我解决了 - 答案是:

it was a server thing. thanks any way. you just enable this extension: php_fileinfo.dll in the php.ini file. then restart the server.

这是服务器的事情。不管怎么说,还是要谢谢你。您只需在 php.ini 文件中启用此扩展名:php_fileinfo.dll。然后重启服务器。

回答by rdiz

Remove the headers, they're not necessary. In L5, you can do

删除标题,它们不是必需的。在L5,你可以做

return response()->download($file, "Ahmed Badawy - CV.pdf");

Please read the docs.

阅读文档