2

Hi I'm trying to fix a Laravel download bug I am dealing with. I have the correct route setup, and correct function in the controller. I also am able to confirm that I have access to the file because I created a file using the exact same route and returned it. By doing this I was able to successfully return the contents of the file. However when I try and use a 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 resulted in this error:

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

What I have been able to surmise is that I am getting a 500 HTTP error. My inspection does not provide me with any other information however. Any idea what is going on?

NSaid
  • 689
  • 2
  • 16
  • 32
  • Do the file exists? What's var_dump(File::get('../resources/logs/' . $id)) ? – jakub_jo Apr 05 '16 at 22:22
  • 1
    when you inspect the ` – KDaker Apr 05 '16 at 22:26
  • @jakub_jo string(59048) "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... plus all of the contents of the file – NSaid Apr 05 '16 at 22:29
  • 1
    KDaker's comment should get you on the right track. Your $now variable is probably not what you think it is. The error makes it look like you are passing something very long and weird instead of an ID. – Lucha Laura Hardie Apr 05 '16 at 22:29
  • @KDaker localhost/getDownload/2016-04-05. The file does exist on my server and the route exists as well: Route::get('getDownload/{id}', array('uses' => 'HonoursController@getDownload')); – NSaid Apr 05 '16 at 22:31
  • @LuchaLauraHardie $now is declared like this: $now = date("Y-m-d", strtotime(\Carbon\Carbon::now())); in the show function of the Controller and passed to the view – NSaid Apr 05 '16 at 22:33
  • so if you copy this link directly in the browser it works? if not, can you show us a route that works? – KDaker Apr 05 '16 at 22:34
  • No when I copy the link directly into the browser I get presented with the error above. – NSaid Apr 05 '16 at 22:38

3 Answers3

2

Try this:

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

Community
  • 1
  • 1
nick.graziano
  • 677
  • 6
  • 12
1

The first argument of the download method should be a path to the file, not the file itself.

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

Community
  • 1
  • 1
jakub_jo
  • 1,494
  • 17
  • 22
0

Follow these two steps:

  1. Find the details of the file by using the Model.
  2. Use Laravel Response to download file with the help of headers.

Here Blog is the model and $id is the primary key which indicates file. Our table's column name where file name has been stored is cover_image, so $file_name=$blog->cover_image; provides us the file name. Let's assume our file is present in upload/images/ of 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']); ` 

View

Here, ['id'=>1] indicates we are going to download file having primary key as 1. If you want to download another just change with any n integer number.

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

`

Ramesh KC
  • 589
  • 4
  • 10