如何在laravel 5.4中下载文件

时间:2017-12-17 16:15:06

标签: laravel

如何在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

1 个答案:

答案 0 :(得分:0)

请查看Route Parameters

您应该修改CourseController.php

public function download($episode)
{
    ...
    $episode = Episode::find($episode);
    if ($episode) {
    }
    else {
    }
    ....
}