如何在laravel 5.4中下载文件

问题描述 投票:0回答:1

如何在laravel 5.4中下载文件

我必须在laravel app中下载一个文件并从中提取一些值并在我的视图中使用它。你会为这种情况推荐什么方法?

course.blade.php

<tbody>
@foreach($course->episodes()->latest()->get() as $episode)
    <tr>
        <th>{{ $episode->number }}</th>
        <td>{{ $episode->title }}</td>
        <td>{{ $episode->time }}</td>
        <td>
            <a href="{{ $episode->download() }}"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span></a>
        </td>
    </tr>
@endforeach
</tbody>

CourseController

public function download(Episode $episode)
{
    return $episode;
}

web.php

Route::get('/download/{episode}', 'CourseController@download');

模型

public function download()
    {
        if(! auth()->check()) return '#';

        $status = false;
        switch ($this->type) {
            case 'free' :
                $status = true;
                break;
            case 'vip' :
                if(user()->isActive()) $status = true;
                break;
            case 'cash' :
                if(user()->checkLearning($this->course)) $status = true;
                break;
        }
        $timestamp = Carbon::now()->addHours(5)->timestamp;
        $hash = Hash::make('fds@#T@#56@sdgs131fasfq'.$this->id.request()->ip().$timestamp);

        return $status ? "/download/$this->id?mac=$hash&t=$timestamp" : "#";
    }

我试过但失败了。

Error sample

laravel
1个回答
0
投票

请查看Route Parameters

您应该修改CourseController.php

public function download($episode)
{
    ...
    $episode = Episode::find($episode);
    if ($episode) {
    }
    else {
    }
    ....
}
© www.soinside.com 2019 - 2024. All rights reserved.