Action button that can download file.

I created an action button that uses an action class which returns a response()->download() that should download a file when clicked. It returns a successful notification but the download wont start.
No description
No description
16 Replies
charlie
charlie3mo ago
The download instruction looks fine, is the $zipFilePath correct?
Karl Marx
Karl MarxOP3mo ago
yes. if I use a controller for the action class it works
Dennis Koch
Dennis Koch3mo ago
You don't return anything from your action.
Karl Marx
Karl MarxOP3mo ago
Dont return anything from the action button?
Dennis Koch
Dennis Koch3mo ago
The action closure returns void. You should return the response you are creating in.
Karl Marx
Karl MarxOP3mo ago
Thanks @Dennis Koch
Solution
Karl Marx
Karl Marx3mo ago
No description
Dennis Koch
Dennis Koch3mo ago
BTW I don’t think the isSucessful makes sense. It probably just checks the return code of the response which should always be 200
Karl Marx
Karl MarxOP3mo ago
Just returning the response better?
Dennis Koch
Dennis Koch3mo ago
Does response()->download() send a 404 when the file is not there? Or does it throw an exception?
Karl Marx
Karl MarxOP3mo ago
It throws an exception,
taka92
taka923mo ago
you can check something to fix it 1. ensure correct response type 2. Action class Structure
public function handle() { $filePath = storage_path('app/public/file_to_download.pdf'); if (file_exists($filePath)) { return response()->download($filePath); } return back()->with('error', 'File not found.'); }
3. check for Redirects or Response Modifications
Dennis Koch
Dennis Koch3mo ago
Then a try catch would be better than your current if
Karl Marx
Karl MarxOP3mo ago
No description
Karl Marx
Karl MarxOP3mo ago
Like this?
Dennis Koch
Dennis Koch3mo ago
Success probably doesn’t make sense. In case of error it sends both then. Otherwise yes

Did you find this page helpful?