-
Notifications
You must be signed in to change notification settings - Fork 223
/
file-downloads.blade.php
68 lines (57 loc) · 1.57 KB
/
file-downloads.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
* [Introduction](#introduction)
* [Testing File Downloads](#testing-file-downloads)
## Introduction {#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.
@component('components.code-component')
@slot('class')
@verbatim
class ExportButton extends Component
{
public function export()
{
return Storage::disk('exports')->download('export.csv');
}
}
@endverbatim
@endslot
@slot('view')
@verbatim
<button wire:click="export">
Download File
</button>
@endverbatim
@endslot
@endcomponent
Livewire should handle any file download that Laravel would. Here are a few other utilities you might use:
@component('components.code', ['lang' => 'php'])
@verbatim
return response()->download(storage_path('exports/export.csv'));
@endverbatim
@endcomponent
@component('components.code', ['lang' => 'php'])
@verbatim
return response()->streamDownload(function () {
echo 'CSV Contents...';
}, 'export.csv');
@endverbatim
@endcomponent
## Testing File Downloads {#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.
@component('components.code-component', [
'className' => 'ExportDownloadedTest.php',
])
@slot('class')
@verbatim
/** @test */
public function can_download_export()
{
Livewire::test(ExportButton::class)
->call('download')
->assertFileDownloaded('export')
;
}
@endverbatim
@endslot
@endcomponent