dongying3744 2016-12-10 02:22
浏览 54

Laravel 5.2 - 从数据库下载文件

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

  • 写回答

2条回答 默认 最新

  • dongmou5628 2016-12-10 02:29
    关注

    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));
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致