These are what I have :
Database table named lamanInformasi
, which has these fields: id, judul, isi, created_at, updated_at.
This is what I want :
User can upload document and the files will be stored to database. The file names will be saved to isi
field, and the files itself will be saved to a folder named propic. I've done all of them correctly. Then, I have a problem. When the data from database is showed, there are links in each file name. When user clicks the link, the file will be automatically downloaded. How to make it possible? When I click the link, I have this error: NotFoundHttpException
These are my codes :
index.blade.php -- I put this file inside upload folder
<table class="table table-striped table-bordered" border= "1px solid black">
<thead>
<tr>
<td>ID</td>
<td>Judul</td>
<td>Isi</td>
<td>Created At</td>
<td>Updated At</td>
</tr>
</thead>
<tbody>
@foreach($lamanInformasi as $file)
<tr>
<td>{{$file->id}}</td>
<td>{{$file->judul}}</td>
<td><a href="{{URL::to('upload/' . $file->id)}}">{{$file->isi}}</a></td>
<td>{{$file->created_at}}</td>
<td>{{$file->updated_at}}</td>
</tr>
@endforeach
</tbody>
</table>
LamanInformasiController
public function show($id)
{
$lamanInformasi = $this->model->whereId($id)->firstOrFail();
$downloadFile = response()->download($lamanInformasi->filepath, $lamanInformasi->name);
return view('upload.index', compact('lamanInformasi','downloadFile'));
}
Thanks for your help
The 'NotFoundHttpException' means Laravel wasn't able to find a route to for the request. So try it
<td><a href="{{URL::to('upload')}}/{{ $file->id}}">{{$file->isi}}</a></td>
On controller
return view('upload.index', array('lamanInformasi'=>$lamanInformasi,'downloadFile'=>$downloadFile));
just use download={{$report->name}}
download={{$report->name}}
-It will make the download file name download="filename", "filename" becomes the name of the downloaded file. and force to download and href tells where the file is
in controller
public function download()
{
//display all types of reports from database name downloads
$reports = DB::table('downloads')->all();
return view('reports.proposal',compact('reports'));
}
and my view with a link to download is like this
@foreach($reports as $report)
<a href="../backend/uploads/{{$report->name}}" download="{{$report->name}}">{{$report->name}}</a>
@endforeach
youtube link:https://www.youtube.com/watch?v=AlnackyPJPY
gitlab link:https://gitlab.com/Bons/download-files-laravel5