简体   繁体   English

Laravel响应::单击按钮后下载不起作用并且pdf文件未下载

[英]Laravel Response::download not working and pdf file not downloaded when clicked button

I'm trying to achieve a link in a menu bar, such that when a user clicks on the link, a pdf file will be downloaded automatically, and it should not navigate to other pages. 我正在尝试在菜单栏中实现链接,以便当用户单击链接时,将自动下载pdf文件,并且不应导航到其他页面。

Inside my main.blade.php, consists of a menu bar, I have this link: 在我的main.blade.php中,它包含一个菜单栏,我具有以下链接:

<a href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/php/timetable/public/download">Help</a>

where $_SERVER['SERVER_NAME'] is localhost. 其中$ _SERVER ['SERVER_NAME']是本地主机。

Inside my routes.php: 在我的routes.php中:

Route::get('/download', array('uses'=>'MainController@getDownloadHelp'));

Inside my controller called MainController: 在我的控制器MainController中:

public function getDownloadHelp()
{
    $file= public_path(). "/public/download";
    $filename = 'help.pdf';
    $headers = array(
          'Content-Type' => 'application/pdf',
        );
    return Response::download($file, $filename, $headers);
}

The PDF file is stored under /public/download/help.pdf PDF文件存储在/public/download/help.pdf下

The problem I'm facing right now is, when I clicked the 'Help' link on the menu bar, it redirects me to localhost/download which is not what I wanted. 我现在面临的问题是,当我单击菜单栏上的“帮助”链接时,它将我重定向到localhost / download ,这不是我想要的。 And also, the pdf is not downloaded. 而且,不会下载pdf。

I really need some help here! 我真的需要一些帮助! Where and what did I went wrong? 我哪里出了什么问题?

You can try to use {{ URL::to('/download') }} instead of $_SERVER['SERVER_NAME']... or in your blade 您可以尝试使用{{ URL::to('/download') }}代替$_SERVER['SERVER_NAME']...或在刀片中使用

echo link_to('download', $title, $attributes = array(), $secure = null);

it will write all the <a></a> thing 它将写所有<a></a>

for the path in the controller maybe you can remove the public in the path of the file like this 对于控制器中的路径,也许您可​​以像这样删除文件路径中的public

public_path() . '/download/';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM