【发布时间】:2017-04-25 14:06:41
【问题描述】:
这些是我所拥有的:
名为lamanInformasi的数据库表,包含以下字段:id、judul、isi、created_at、updated_at。
这就是我想要的:
用户可以上传文件,文件将被存储到数据库中。文件名将保存到isi 字段,文件本身将保存到名为 propic 的文件夹中。我已经正确地完成了所有这些。然后,我有一个问题。当显示来自数据库的数据时,每个文件名中都有链接。当用户点击链接时,文件将被自动下载。如何使它成为可能?当我单击链接时,出现此错误:NotFoundHttpException
这些是我的代码:
index.blade.php -- 我把这个文件放在上传文件夹中
<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>
拉曼信息控制器
public function show($id)
{
$lamanInformasi = $this->model->whereId($id)->firstOrFail();
$downloadFile = response()->download($lamanInformasi->filepath, $lamanInformasi->name);
return view('upload.index', compact('lamanInformasi','downloadFile'));
}
感谢您的帮助
【问题讨论】:
-
塞拉马特帕吉。如果
href指向正确的目录,您可以在控制台上检查。 :) -
@LaraBelle Selamat pagi。如何检查?
-
f12 打开控制台。然后尝试找到带有链接的
anchor<a href="must/be/on/upload/srcoffile">标签。 -
我找不到
anchor标签 -
我很困惑你想如何实现它。您可以通过将 href 指向更好的文件来实现它。但是,您可以使用 laravel 方法来实现它。你想用什么方法?
标签: php database laravel file-upload laravel-5.2