You're browsing the documentation for an old version of Livewire. Consider upgrading your project to Livewire 3.x.

File Downloads

Be amazing at Livewire
with our in-depth screencasts.
Watch Now

Introduction

Livewire supports triggering file downloads for users with a simple, intuitive API.

To trigger a file download, you can return a Laravel file download from any component action.

            
1class ExportButton extends Component
2{
3 public function export()
4 {
5 return Storage::disk('exports')->download('export.csv');
6 }
7}
            
1<button wire:click="export">
2 Download File
3</button>

Livewire should handle any file download that Laravel would. Here are a few other utilities you might use:

        
1return response()->download(storage_path('exports/export.csv'));
        
1return response()->streamDownload(function () {
2 echo 'CSV Contents...';
3}, 'export.csv');

Testing File Downloads

Testing file downloads is simple with livewire.

Here is an example of testing the component above and making sure the export was downloaded.

            
1/** @test */
2public function can_download_export()
3{
4 Livewire::test(ExportButton::class)
5 ->call('download')
6 ->assertFileDownloaded('export')
7 ;
8 
9}
← Previous Topic File Uploads
Next Topic → Query String