2016-04-05 21 views
2

こんにちは、私はLaravelのバグを修正しようとしています。私は正しいルート設定と、コントローラの正しい機能を持っています。まったく同じルートを使用してファイルを作成して返したため、ファイルにアクセスできることも確認できました。これを行うことで、ファイルの内容を正常に返すことができました。しかし、私は試してみて、ビューからボタンを使用して、コントローラの関数を呼び出すとき、私はこのエラーを取得:Laravel - ファイルのダウンロード

FileNotFoundException in File.php line 37: 
The file "The file "2016-04-04_07-21-50 - Pinging host: 192.168.2.1 
2016-04-04_07-21-50 - Host 192.168.2.1 is up! 
2016-04-04_07-21-50 - Pinging host: 192.168.2.2 
2016-04-04_07-21-53 - Pinging host: 192.168.2.3 ... 

今ここで、このエラーが生じたコードです:

show.blade.phpは

<a class="btn btn-default col-md-12" href="/getDownload/{{ $now }}" role="button">Download Today's Log</a> 

HonoursController.php

public function getDownload($id) 
    { 
     $file = File::get("../resources/logs/$id"); 
     $headers = array(
      'Content-Type: application/octet-stream', 
    ); 
     #return Response::download($file, $id. '.' .$type, $headers); 
     return response()->download($file, $id.'txt', $headers); 
    } 

私が推測できることは、私は500 HTTPエラーを取得しているということです。私の検査では、私に他の情報は与えられていません。何が起こっているか考えてみませんか?

+0

は、ファイルが存在していますか? var_dump(File :: get( '../ resources/logs /'。$ id))とは何ですか? –

+1

ブラウザで ' KDaker

+0

@jakub_jo string(59048) "2016-04-04_07-21-50 - pinging host:192.168.2.1 2016-04-04_07-21-50 - ホスト192.168.2.1が稼動しています!2016-04-04_07-21-50 - Pinging host:192.168.2.2 ...ファイルの内容をすべて加えた – NSaid

答えて

2

これを試してみてください:ドキュメントから

public function getDownload($id) 
{ 
    // $file = File::get("../resources/logs/$id"); 
    $headers = array(
     'Content-Type: application/octet-stream', 
    ); 
    #return Response::download($file, $id. '.' .$type, $headers); 
    return response()->download("../resources/logs/$id", $id.'txt', $headers); 
} 

を:

The download method may be used to generate a response that forces the user's browser to download the file at the given path.

return response()->download($pathToFile, $name, $headers);

https://laravel.com/docs/5.1/responses#basic-responses

1

ダウンロードメソッドの最初の引数は、ファイル自体ではなく、ファイルへのパスにする必要があります。

The download method may be used to generate a response that forces the user's browser to download the file at the given path. ...

Source: https://laravel.com/docs/5.2/responses#file-downloads

関連する問題