Skip to content

Instantly share code, notes, and snippets.

@wmandai
Forked from nkeena/Download.php
Created June 23, 2021 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmandai/cc3a05bae360c1267cebd17e32997182 to your computer and use it in GitHub Desktop.
Save wmandai/cc3a05bae360c1267cebd17e32997182 to your computer and use it in GitHub Desktop.
Download a file using Laravel Livewire
<div>
<button wire:click="download">Download</button>
</div>
// include this script block after the livewire scripts
<script>
window.livewire.on('startDownload', path => {
// open the download in a new tab/window to
// prevent livewire component from freezing
window.open('download/' + path, '_blank');
});
</script>
<?php
use Livewire\Component;
class Download extends Component
{
public function download()
{
// get the path to the file
$path = "";
// emit an event with the path
$this->emit('startDownload', $path);
}
public function render()
{
return view('download');
}
}
<?php
Route::get('download/{path}', function($path) {
return Illuminate\Support\Facades\Storage::download($path);
})->where('path', '.*');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment