Laravel存储门面和检索文件

时间:2016-04-10 16:10:03

标签: storage laravel-5.2 flysystem

如何获取位于存储中的文件?

我构建了一条路线并将其定向到myCcontroller@myMethod。方法如下

public function myMethod()
{
    $validPath = 'valid-path-inside-local-storage';
    $file = Storage::disk(env('STORAGE_TO_USE','local'))->get($validPath);
    return $file;
}

但我得到了胡言乱语,可能是原始数据。如果文件是图像,如何让浏览器显示图像?如果文件是a.xls,我如何下载它?

2 个答案:

答案 0 :(得分:0)

{{1}}

我希望我没有错过任何东西。

答案 1 :(得分:0)

为您的路线定义:

Route::get('docs/{file}', 'DocumentController@getDoc');

这是你的控制器

class DocumentController extends BaseController
{
    public function getDoc($file){
      $path = 'pathToDirectory/'.$file;
      if(Storage::disk('yourDisk')->exists($path)){
        $attachment = Storage::disk('yourDisk')->get($path);
        $type = Storage::disk('yourDisk')->mimeType($path);
        return Response::make($attachment, 200)->header("Content-Type", $type);
      }else{
        return Response::json('This file does not exists.');
      }
    }
}