Фасад Laravel Storage и извлечение файлов

Как получить файлы, расположенные в хранилище?

Я построил маршрут и направил его на 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?


person dbr    schedule 10.04.2016    source источник


Ответы (2)


    $validPath = 'valid-path-inside-local-storage';
    $mimeType = Storage::mimeType($validPath);
    $file = Storage::disk(env('STORAGE_TO_USE','local'))->get($validPath);
    return (new Response($file, 200))
        ->header('Content-Type', $mimeType);

Надеюсь, я ничего не пропустил.

person dbr    schedule 11.04.2016

Для вашего маршрута определите это:

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.');
      }
    }
}
person Ravi Kumar    schedule 24.06.2017