Download a generated file with Action button

Is there a simple way to download a file by pressing an action button? My code already prepare a proper response with the correct mime type and body but the request is not handled correctly by the browser. If I cannot use a direct download I need to use a URL but I need to send POST inputs to the request as I'm generating a PDF preview of the form.
5 Replies
LeandroFerreira
LeandroFerreira4mo ago
->action(fn() => Storage::download('file.jpg'))
->action(fn() => Storage::download('file.jpg'))
?
hitech95
hitech95OP4mo ago
Its a stream tho. Its not a stored file I'm taking the form data via $get and passing it to a service that generate a PDF the pdf is not stored. i've tried with the livewire:
response()->streamDownload(fn() => mycalltoservice(), $get('name').'.pdf')
response()->streamDownload(fn() => mycalltoservice(), $get('name').'.pdf')
Oxalate
Oxalate4mo ago
When using this for a file for a model, it says:
unable to retrieve the file_size for file at location sd02380jsf.jpg for example.
awcodes
awcodes4mo ago
Are you trying to download it before persisting it to storage? Can you share the code of your actual action?
hitech95
hitech95OP4mo ago
Yes, it is a http response with proper headers set. As it is a preview/draft it is not stored on the storage. I can provide an example! The code is basically:
Action::make('download-draft')
->label('Download draft')
->action(function ($get) {
$mpdf = new \Mpdf\Mpdf();
// This uses the $get to generate a proper page
$mpdf->WriteHTML('Hello World');

// Sends output inline to browser
$mpdf->Output();

// I also tried with:
response()->streamDownload(fn() => $mpdf->Output($get('name').'.pdf', Destination::INLINE)

});
Action::make('download-draft')
->label('Download draft')
->action(function ($get) {
$mpdf = new \Mpdf\Mpdf();
// This uses the $get to generate a proper page
$mpdf->WriteHTML('Hello World');

// Sends output inline to browser
$mpdf->Output();

// I also tried with:
response()->streamDownload(fn() => $mpdf->Output($get('name').'.pdf', Destination::INLINE)

});
@Leandro Ferreira If necessary I can update my previous repo demo with such example.
Want results from more Discord servers?
Add your server