使用Response :: download在laravel中下载文件

在Laravel应用程序中,我试图在视图内部实现一个按钮,该按钮可以允许用户下载文件而无需导航至任何其他视图或路径现在我有两个问题:(1)函数抛出以下

The file "/public/download/info.pdf" does not exist

(2)“下载”按钮不应将用户导航到任何地方,而应仅在同一视图上下载文件,即“我的当前设置”,将视图路由到“ / download”

我正在尝试实现以下方法:

按键:

  <a href="/download" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>

路线:

Route::get('/download', 'HomeController@getDownload');

控制器:

public function getDownload(){

//PDF file is stored under project/public/download/info.pdf

$file="./download/info.pdf";

return Response::download($file);

}

回答:

尝试这个。

public function getDownload()

{

//PDF file is stored under project/public/download/info.pdf

$file= public_path(). "/download/info.pdf";

$headers = array(

'Content-Type: application/pdf',

);

return Response::download($file, 'filename.pdf', $headers);

}

"./download/info.pdf"将无法正常工作,因为您必须提供完整的物理路径。

Laravel 5、5.1、5.2或5. *用户可以使用以下方法代替ResponseFacade。但是,我先前的答案对Laravel

4或5都适用。(将$header数组结构更改为关联数组=>-删除“ Content-Type”后的冒号-

如果我们不执行这些更改,则将以错误的方式添加标头:标头名称将从0,1,…开始。

$headers = [

'Content-Type' => 'application/pdf',

];

return response()->download($file, 'filename.pdf', $headers);

以上是 使用Response :: download在laravel中下载文件 的全部内容, 来源链接: utcz.com/qa/432148.html

回到顶部