Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cooperino's avatar

How to actually make the button with link download the file?

I am currently creating a file from s3 storage:

            $headers = [
                'Content-Type' => 'text/xlsx',
            ];

            return Response::make(Storage::disk('s3')->get($path), '200', $headers);

And send the link to email. When I click on the link it just opens an Untitled tab.

however if I right click the button and copy the button link, the file downloads.

How to make it download directly by clicking the button?

0 likes
19 replies
Sinnbeck's avatar

Add the download tag

<a href="yoururl" download>Download here!</a>
cooperino's avatar

@Sinnbeck I set it from my email markdown:

@component('mail::button', ['url' => $url])
        Download
@endcomponent
cooperino's avatar

@Sinnbeck yes a link from email which redirects to the download method in my controller

cooperino's avatar

@Nakov Yes but still it opens up Chrome in an Untitled blank tab

Also, there seem to be something else that's the issue, because if I right click the button and then "open in new tab".. it downloads it

Nakov's avatar

@cooperino and if you click on the link which is generated at the footer.. in case the button does not work, what happens then?

And of course it will be an empty page btw, since there is no other response.. But does the download happen anyway?

you can do the following:

Storage::disk('s3')->download($path);

return redirect()->route('some named route showing success message or at the home page');
1 like
cooperino's avatar

@Nakov Got it regarding the empty page, but when I click the link instead of the button then it opens the browser also in a blank.

Only when I copy paste the link in the browser then it downloads the file and closes the tab quickly

I even tried to add

Content-Disposition' => 'attachment; filename="'. $file_name.'"',

but that didn't work

Nakov's avatar

@cooperino And what about:

return Response::download(Storage::disk('s3')->url($path));

I believe the reason for the blank page is that the file is kept in an external storage like s3, and the request is not initiated for downloading the file when clicking on the mail. But I might be wrong.. Try the above.

cooperino's avatar

@Nakov Now it shows the full s3 path saying the file does not exist (The file <aws_full_s3_path>/folder/file_name does not exist)

but it does exist because when I use: Response::make(Storage::disk('s3')->get($path) then it does download the file (when copy pasting the link)

At least the behavior is not just blank page anymore

Nakov's avatar

@cooperino that's a bit weird.. what does this dd(Storage::disk('s3')->get($path)) prints out? Is it a correct URL ?

1 like
cooperino's avatar

@Nakov I noticed something weird: I brought the code back to : return Response::make(Storage::disk('s3')->get($path), '200', $headers);

And then from my mail client I click the download button. It opens a new tab with Untitled page (about:blank as url) . Then I just switch back to my mail tab, and switch back to the Untitled tab and instead of Untitled i now see the correct link

Update: the dd just outputs the file contents in the browser

cooperino's avatar

Update: So after the "Untitled" page opens, if I click "refresh" then it downloads the file and closes the tab.. something is holding the page waiting for some refresh

cooperino's avatar

@Nakov Tried Edge but same behavior (They are pretty much identical browsers), and I don't have access to Firefox here

Nakov's avatar

@cooperino let's add some magic then:

$headers = [
    'Content-Type' => 'text/xlsx',
	'Refresh' => '0',
];

return Response::make(Storage::disk('s3')->get($path), '200', $headers);
1 like
Nakov's avatar

@cooperino try it with the response()->download() or the Storage::download too. Those are my last comments on this thread, I cannot test it, so I don't know what is going on there.

1 like
cooperino's avatar

@Nakov just updating that it didn't work either :), Nonetheless thank you very much for your help

Please or to participate in this conversation.