在 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"
将不起作用,因为您必须提供完整的物理路径。
更新2016年5月20日
Laravel 5、5.1、5.2 或 5.* 用户可以使用以下方法代替
Response
门面。但是,我之前的答案适用于 Laravel 4 或 5。($header
数组结构更改为关联数组=>
-删除“Content-Type”后的冒号-如果我们不进行这些更改,则标题将以错误的方式添加:标题的名称将从0,1,...开始的数字)
$headers = [
'Content-Type' => 'application/pdf',
];
return response()->download($file, 'filename.pdf', $headers);
Laravel 5 中的文件下载非常简单。
正如 @Ashwani 提到的,Laravel 5 允许使用response()->download()
返回文件进行下载。我们不再需要弄乱任何标头。要返回文件,我们只需:
return response()->download(public_path('file_path/from_public_dir.pdf'));
从控制器内部。可重复使用的下载路径/控制器
现在让我们创建一个可重用的文件下载路由和控制器,以便我们可以在 public/files
创建控制器:
php artisan make:controller --plain DownloadsController
在
app/Http/routes.php
中创建路线:
Route::get('/download/{file}', 'DownloadsController@download');
在app/Http/Controllers/DownloadsController
中制作下载方法:
class DownloadsController extends Controller
{
public function download($file_name) {
$file_path = public_path('files/'.$file_name);
return response()->download($file_path);
}
}
现在只需将一些文件放入 public/files
目录中,您就可以通过链接到 /download/filename.ext
:将它们提供给服务器
<a href="/download/filename.ext">File Name</a> // update to your own "filename.ext"
如果你引入了 Laravel Collective 的 Html 包,你可以使用 Html 外观: 在接受的答案中,对于 Laravel 4, headers 数组的构造不正确。用途:
$headers = array(
'Content-Type' => 'application/pdf',
);
其中相当多的解决方案建议引用 Laravel 应用程序的 public_path() 来定位文件。有时您需要控制对文件的访问或提供对文件的实时监控。在这种情况下,您需要保持目录私有并限制控制器类中的方法的访问。以下方法应该对此有所帮助:
return response()->download($pathToFile);
.
如果您使用
Fileentry
,您可以使用以下功能进行下载。
// download file
public function download($fileId){
$entry = Fileentry::where('file_id', '=', $fileId)->firstOrFail();
$pathToFile=storage_path()."/app/".$entry->filename;
return response()->download($pathToFile);
}
我认为你可以使用$file= public_path(). "/download/info.pdf";
$headers = array(
'Content-Type: ' . mime_content_type( $file ),
);
有了这个你就可以确定这是一个pdf文件。
HTML href 链接点击:
<a ="{{ route('download',$name->file) }}"> Download </a>
在控制器中:
public function download($file){
$file_path = public_path('uploads/cv/'.$file);
return response()->download( $file_path);
}
在途:
Route::get('/download/{file}','Controller@download')->name('download');
// 尝试此下载任何文件。拉拉维尔 5.*
public function getDownload()
{
//PDF file is stored under project/public/download/info.pdf
$file= public_path(). "/download/info.pdf";
return response()->download($file);
}
HTML link click
<a class="download" href="{{route('project.download',$post->id)}}">DOWNLOAD</a>
// Route
Route::group(['middleware'=>['auth']], function(){
Route::get('file-download/{id}', 'PostController@downloadproject')->name('project.download');
});
public function downloadproject($id) {
$book_cover = Post::where('id', $id)->firstOrFail();
$path = public_path(). '/storage/uploads/zip/'. $book_cover->zip;
return response()->download($path, $book_cover
->original_filename, ['Content-Type' => $book_cover->mime]);
}
<a href="{{route('download',$details->report_id)}}" type="button" class="btn btn-primary download" data-report_id="{{$details->report_id}}" >Download</a>
这是路线:
Route::get('/download/{id}', 'users\UserController@getDownload')->name('download')->middleware('auth');
这是功能:
public function getDownload(Request $request,$id)
{
$file= public_path(). "/pdf/"; //path of your directory
$headers = array(
'Content-Type: application/pdf',
);
return Response::download($file.$pdfName, 'filename.pdf', $headers);
}
您可以简单地在控制器内部使用:
如果您想使用 JavaScript 下载功能,那么您也可以这样做
<a onclick="window.open('info.pdf) class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>
另请记住将 info.pdf 文件粘贴到项目的公共目录中