Laravel - Downloads

Hi I am trying to fix the Laravel loading error I am dealing with. I have the correct route setup and the correct function in the controller. I can also confirm that I have access to the file because I created the file using the same route and returned it. By doing this, I was able to successfully return the contents of the file. However, when I try to use the button from the view and call the controller function, I get this error:

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 ...

      

Now, here is the code that led to this error:

show.blade.php

<a class="btn btn-default col-md-12" href="/getDownload/{{ $now }}" role="button">Download Today 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);
    }

      

What I guessed is that I am getting HTTP 500 error. However, my inspection does not provide me with any other information. Any idea what's going on?

+2


source to share


3 answers


Try the following:

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);
}

      

From the docs:



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

return response () → download ($ pathToFile, $ name, $ headers);

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

+2


source


The first argument to the upload method must be the path to the file, not the file itself.



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

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

+1


source


Follow these two steps:

  1. Find file details using model.
  2. Use Laravel Response to load file using headers.

Here Blog is the model and $ id is the primary key that points to the file. The name of the column in our table where the filename was saved is cover_image, so $ file_name = $ blog-> cover_image; provides us with the filename. Suppose our file is present in the upload / images / public folder of Laravel.

controller

'public function download(Blog $blog,$id){
    $blog=$blog->find($id);
    $headers = array(
        'Content-Type: application/octet-stream',
     );
    $pathToFile=public_path('upload/images/');
    $file_name=$blog->cover_image;
    $download_name='Download-'.$file_name;
    return response()->download($pathToFile.$file_name, $download_name, $headers);
 }'

      

route

'Route::get('{id}/file-download',['as'=>'file-download','uses'=>'BlogsController@download']); ' 

      

Look

Here ['id' => 1] indicates that we are going to download a file that has a primary key as 1. If you want to download another one, just change it to any n integer.

'<a class="btn btn-primary" href="{{ route('file-download',['id'=>1]) }}">Download</a>

      

'

0


source







All Articles