2016-04-10 10 views
0

ファイルをStorageに保存するにはどうすればよいですか?Laravelストレージファサードとファイルの取得

私はルートを構築し、それを[email protected]に向けました。方法は以下のとおりです

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

しかし、私はおそらく生データを取得します。ファイルが画像の場合はブラウザにイメージを表示させ、ファイルがたとえばa.xlsの場合はダウンロードするにはどうすればよいですか?

答えて

0
$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); 

私は何も見逃したくないと思う。

0

あなたのルートは、この定義のために:

Route::get('docs/{file}', '[email protected]'); 

をそしてこれはあなたのコントローラ

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.'); 
     } 
    } 
} 
関連する問題