从服务器下载文件(JPG和PDF)-Laravel 5.6

时间:2018-07-22 17:07:57

标签: php laravel

我能够将jpg和pdf上传到我的服务器(数据库)。现在,我希望能够在单击视图中的文件本身时下载它,如下所示。

当我单击文件时,它会将我带到下载页面,并说找不到页面并且文件没有下载

我在做什么不对?

控制器

   public function upload(Request $request)
    {
        $storeFiles = new File;

        $uniqueFileName = uniqid() . Input::file('upload_file')->getClientOriginalName() . '.' . Input::file('upload_file')->getClientOriginalExtension();

        Input::file('upload_file')->move(public_path('/public/files') . $uniqueFileName);

        $storeFiles->path = $uniqueFileName;
        $storeFiles->description = Input::get('description');
        $storeFiles->save();
        return redirect()->back()->with('status', 'File uploaded successfully.');
    }





 public function download($filename)
    {
    $file_path = public_path('/public/files'). $filename;
    if (file_exists($file_path))
    {
        // Send Download
        return Response::download($file_path, $filename, [
            'Content-Length: '. filesize($file_path)
        ]);
    }
    else
    {
        // Error
        exit('Requested file does not exist on our server!');
    }

    }

路线

   Route::get('/course/download/{{file?}}', 'FileController@download')->name('course.download');

查看

      <td><a  href="{{ URL::to( '/course/download/' . $file->path)  }}">{{$file->path}}</a></td>

0 个答案:

没有答案