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.16 Replies
The download instruction looks fine, is the
$zipFilePath
correct?yes. if I use a controller for the action class it works
You don't return anything from your action.
Dont return anything from the action button?
The action closure returns void. You should return the response you are creating in.
Thanks @Dennis Koch
Solution
BTW I don’t think the isSucessful makes sense. It probably just checks the return code of the response which should always be 200
Just returning the response better?
Does
response()->download()
send a 404 when the file is not there? Or does it throw an exception?It throws an exception,
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 ModificationsThen a try catch would be better than your current if
Like this?
Success probably doesn’t make sense. In case of error it sends both then. Otherwise yes