[英]Download files in laravel using Response::download
在 Laravel 应用程序中,我试图在视图中实现一个按钮,允许用户下载文件而无需导航到任何其他视图或路由 现在我有两个问题:(1)下面的函数抛出
The file "/public/download/info.pdf" does not exist
(2) 下载按钮不应将用户导航到任何地方,而应仅在同一视图中下载文件,我当前的设置,将视图路由到“/下载”
这是我试图实现的方式:
按钮:
<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"
将不起作用,因为您必须提供完整的物理路径。
更新 20/05/2016
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 外观:
{!! Html::link('download/filename.ext', 'File Name') !!}
在接受的答案中,对于 Laravel 4,标头数组的构造不正确。 使用:
$headers = array(
'Content-Type' => 'application/pdf',
);
这些解决方案中有不少建议引用 Laravel 应用程序的 public_path() 以定位文件。 有时您会想要控制对文件的访问或提供对文件的实时监控。 在这种情况下,您需要保持目录私有并通过控制器类中的方法限制访问。 以下方法应该对此有所帮助:
public function show(Request $request, File $file) {
// Perform validation/authentication/auditing logic on the request
// Fire off any events or notifiations (if applicable)
return response()->download(storage_path('app/' . $file->location));
}
您也可以使用其他路径,在Laravel 的辅助函数文档中进行了描述
使用laravel 5
使用此代码,因为您不需要标题。
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.*
// 你需要使用门面 "use Illuminate\\Http\\Response;"
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]);
}
这是 html 部分
<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 文件粘贴到项目的公共目录中
你可以简单地在你的控制器中使用: return response()->download($filePath);
快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.