Add the download tag
<a href="yoururl" download>Download here!</a>
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?
Add the download tag
<a href="yoururl" download>Download here!</a>
@Sinnbeck I set it from my email markdown:
@component('mail::button', ['url' => $url])
Download
@endcomponent
@cooperino I thought this was a webpage? Its a link in an email?
@Sinnbeck yes a link from email which redirects to the download method in my controller
Have you tried using the download method:
return Storage::disk('s3')->download($path);
@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
@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');
@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
@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.
@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
@cooperino that's a bit weird.. what does this dd(Storage::disk('s3')->get($path)) prints out? Is it a correct URL ?
@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
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 did you tried a different browser?
@Nakov Tried Edge but same behavior (They are pretty much identical browsers), and I don't have access to Firefox here
@cooperino let's add some magic then:
$headers = [
'Content-Type' => 'text/xlsx',
'Refresh' => '0',
];
return Response::make(Storage::disk('s3')->get($path), '200', $headers);
@Nakov Did not work either :D
@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.
@Nakov just updating that it didn't work either :), Nonetheless thank you very much for your help
Please or to participate in this conversation.