Force Download in laravel-file download in laravel
In this Blog i m written the code for force download option in a webpage using laravel. Please check the routes.php,controller and view files In this example one image file is stored inside the folder-public/upload_image/mypic.jpg Routes.php
1 2 |
Route::get('filelist', 'fileListController@filelist'); Route::post('filedownload', 'fileListController@filedownload'); |
fileListController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use Response; class fileListController extends Controller { public function filelist() { return view('filelist.filelist'); } public function filedownload() { $path=public_path()."/upload_image/mypic.jpg"; return( Response::download( $path ) ); } } |
filelist.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <div class="container"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <div class="panel panel-default"> <div class="panel-heading">Welcome</div> <div class="panel-body"> <form action="filedownload" method="post"> <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> <button>Download file</button> </form> </div> </div> </div> </div> </div> </head> </html> |