Download files button logic and coding

  Fri 01 / 12 / 2023

  Posted by: Nouman Hanif

Category : Laravel

Tags : laravel , backend

Description

How to create download buttons icons in Laravel like the buttons on this site :

https://www.dawateislami.net/bookslibrary/


and the buttons should also download the related file etc pdf , mp4 and mp3 whatever file is attached and the download button is for download 

means i want to know the full coding and logic of dowload file button in Laravel not only how to make this button in frontend


  Answered by CDL

Sure, for the downloads, you can add font awesome cdn to get the download icon for the button, and on the button, you can submit a form containing the file name which you want to download so pass it as reference and accept it in the controller.


Controller Login:


public function downloadFile($filename)

{

       $filePath = public_path('videos/');

        $fileName = 'laravel course intro.mp4';

        $fileWithPath = $filePath. $fileName;

        if (!file_exists($fileWithPath)) {
            abort('404');
        }

        return response()->download($fileWithPath);

}


Don't remove trailing / after the videos, otherwise it wouldn't work.

Solved
  Comment   Share
1 Like   6 Comments


comment-author
Nouman Hanif Author

thanks , i check and then i tell the status



Reply


commented 8 months ago
comment-author
Hadayat Niazi Super Admin

Good job @Nouman



comment-author
Nouman Hanif Author

Solved جزاكم الله خیرا



comment-author
Hadayat Niazi Super Admin

I have updated the answer with simple stuff, now you can pass the path and name in the existing variable and you will be able to download the files.



comment-author
Nouman Hanif Author

i have made a simple html document : <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Download Attribute</title> </head> <body> <a href="CV 29-11-23.pdf" download="">Download CV</a> </body> </html>



Reply


commented 9 months ago
comment-author
Nouman Hanif Author

جزاكم الله Solved now



comment-author
Hadayat Niazi Super Admin

You are adding the pdf name directly into the href tag which will not be able to locate your file, if you know asset() helper it's used to find the files from the public directory, so place your file in public directory and also move this cv into the public directory, after this your syntax will be this. href="{{ asset('CV 29-11-23.pdf') }}". And yes you need understand the basics of Laravel, then will work for you.



comment-author
Nouman Hanif Author

i think i should learn it later , first i should learn Laravel basics



comment-author
Nouman Hanif Author

In the answer downloadFile($filename) is written , here Inthe $filename variable is used which in not declared before , I will have to declare it myself before and what value I will have to assign , I didn't understand it



Reply


commented 9 months ago
comment-author
Nouman Hanif Author

and further more , i want to ask that what extension i will have to install in visual studio code to add namespace automically in the top of the file ?



comment-author
Nouman Hanif Author

<a href="images/posts/imageName" >Download</a> this thing i know , but how will i get this path in $filename (filename variable in controller file) ?



comment-author
Hadayat Niazi Super Admin

Don't make it too complex, it's very simple. Create a link using href and pass it like this <a href="images/posts/imageName" >Download</a>



comment-author
Nouman Hanif Author

but how will i define my file path here ? href and src are html attributes



comment-author
Hadayat Niazi Super Admin

You will pass the file with path to this $filename variable.



comment-author
Nouman Hanif Author

because i am not expert it takes time to work , i am u can say a beginner in Laravel



Reply


commented 9 months ago
comment-author
Hadayat Niazi Super Admin

no problem, take your time.



comment-author
Nouman Hanif Author

i had not checked therefore i did not press the solved , I am checking , if the problem solves i tell other i further asks for the problem occuring



Reply


commented 9 months ago
comment-author
Nouman Hanif Author

thanks



Reply


commented 9 months ago
comment-author
Hadayat Niazi Super Admin

Also please mark the post as solved if it's solved your problem.



comment-author
Hadayat Niazi Super Admin

You are welcome, if you have more questions you can create multiple posts, I am happy to resolve your issue.