【问题标题】:Laravel 5.2 - Download Files From DatabaseLaravel 5.2 - 从数据库下载文件
【发布时间】: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&lt;a href="must/be/on/upload/srcoffile"&gt;标签。
  • 我找不到anchor标签
  • 我很困惑你想如何实现它。您可以通过将 href 指向更好的文件来实现它。但是,您可以使用 laravel 方法来实现它。你想用什么方法?

标签: php database laravel file-upload laravel-5.2


【解决方案1】:

“NotFoundHttpException”表示 Laravel 无法找到请求的路由。那就试试吧

 <td><a href="{{URL::to('upload')}}/{{ $file->id}}">{{$file->isi}}</a></td>

在控制器上

    return view('upload.index', array('lamanInformasi'=>$lamanInformasi,'downloadFile'=>$downloadFile));

【讨论】:

  • 您如何维护上传文件的路由?
  • 我只有这一条路线:Route::resource('/upload8','LamanInformasiController');
  • return view('upload.index', compact('lamanInformasi','downloadFile'));在这一行中,为什么您使用紧凑功能。使用数组而不是紧凑函数。
  • 返回视图('upload.index', array('lamanInformasi'=>$lamanInformasi,'downloadFile'=>downloadFile));
  • @HikmatSijapati 在 PHP 文档中首先检查 compact。这不是 laravel 功能。 php.net/manual/en/function.compact.php 在数组 la 上有何不同?
【解决方案2】:

只需使用download={{$report-&gt;name}}

download={{$report-&gt;name}} - 会使下载文件名download="filename", “filename”成为下载文件的名称。并强制下载和href告诉文件在哪里

控制器

  public function download()
     {
        //display all types of reports from database name downloads

        $reports = DB::table('downloads')->all();  
       return view('reports.proposal',compact('reports'));
     }

而我的view带有下载链接是这样的

@foreach($reports as $report)

             <a href="../backend/uploads/{{$report->name}}" download="{{$report->name}}">{{$report->name}}</a>

@endforeach

youtube 链接https://www.youtube.com/watch?v=AlnackyPJPY

gitlab 链接https://gitlab.com/Bons/download-files-laravel5

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 2017-04-24
    • 2016-06-29
    • 2017-08-08
    • 2016-09-25
    • 2023-03-09
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多