Rock
Rock

Reputation: 395

Laravel : Download Button not working properly

I'm trying to make user download file from the page. But, whenever I click on the download button, this error appears:

The requested resource /download was not found on this server.

Here's my code.

view:

<a href="/download/file_name" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a> 

Controller:

public function getDownload($file_name) {
$file_path = public_path('files/'.$file_name);
return response()->download($file_path);}

Route:

Route::get('/download/{file}', 'DownloadFileController@getDownload');

and it shows : enter image description here

The error showing is::

http://127.0.0.1:8000/download Failed to load resource: the server responded with a status of 404 (Not Found)

I just don't get it, how to fix this?

and shows

Undefined variable: file_name (View: C:\xampp\htdocs\Land_Map\resources\views\pages\upload.blade.php) (View: C:\xampp\htdocs\Land_Map\resources\views\pages\upload.blade.php)

when I use:

<a href="{{ URL::to('/download/'.$file_name) }}" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>

But, works fine with

<a href="/download/php_tutorial.pdf" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>

But, I want the user to select from several files in the download folder. But, don't know how to do it. Tried several ways, but didn't work.

I also tried:

<a href="{{route('upload')}}" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>

I was actually trying to show the files being uploaded, but didn't work.

But, it shows:

Route [upload] not defined. (View: C:\xampp\htdocs\Land_Map\resources\views\pages\upload.blade.php) (View: C:\xampp\htdocs\Land_Map\resources\views\pages\upload.blade.php)

But, Routes :

Route::get('/upload', 'FormUploadController@upload');

Route::post('upload', 'UploadController@upload'); 

Upvotes: 2

Views: 1163

Answers (3)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

I think your code need to update like:

For controller

public function getDownload($file_name) {
    $file_path = public_path().'/files/'.$file_name;
    return response()->download($file_path);
}

For blade file:

<a href="{{ URL::to('/download/'.$filename) }}"class="btn btn-large pull-right"><i class="icon-download-alt"> </i> Download Brochure </a>

UPDATE Here, make sure that $filename should be passed from your controller view like given into Docs. Hope this work for you!

Upvotes: 1

Marco Moretti
Marco Moretti

Reputation: 689

In href you have to add the filename href="/download/file_name"

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

You need to pass filename:

<a href="/download/Guide.pdf" ....>Download Brochure</a>

If you do not pass filename, Laravel will look for this route:

Route::get('/download', 'DownloadFileController@getDownload'); 

Upvotes: 1

Related Questions