|
| 1 | +--- |
| 2 | +title: File Upload |
| 3 | +sort: 15 |
| 4 | +--- |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +The `file-upload` component provides a custom file input for your forms. It can easily be integrated into your livewire forms |
| 9 | +as well just by adding a `wire:model` to the input. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Even though the `file-upload` component will work out-of-the-box if you're using the script blade directives in your layout (`@fcScripts`), |
| 14 | +we recommend that you install and compile the JavaScript libraries before you deploy to production. |
| 15 | + |
| 16 | +- [Alpine.js](https://github.com/alpinejs/alpine) `^2.7` |
| 17 | + |
| 18 | +## Basic Usage |
| 19 | + |
| 20 | +In its most basic usage, you can use it as a self-closing component and pass it a name: |
| 21 | + |
| 22 | +```html |
| 23 | +<x-file-upload name="avatar" /> |
| 24 | +``` |
| 25 | + |
| 26 | +This will output the following HTML: |
| 27 | + |
| 28 | +```html |
| 29 | +<div class="file-upload space-x-5"> |
| 30 | + <div x-data="{ focused: false, isUploading: false, progress: 0 }" |
| 31 | + class="space-y-4 w-full" |
| 32 | + > |
| 33 | + <span class="file-upload__input"> |
| 34 | + <input x-on:focus="focused = true" |
| 35 | + x-on:blur="focused = false" |
| 36 | + class="sr-only" |
| 37 | + type="file" |
| 38 | + name="avatar" |
| 39 | + id="avatar" |
| 40 | + /> |
| 41 | + |
| 42 | + <label for="avatar" |
| 43 | + x-bind:class="{ 'file-upload__label--focused': focused }" |
| 44 | + class="file-upload__label" |
| 45 | + > |
| 46 | + <span role="button" |
| 47 | + aria-controls="avatar" |
| 48 | + tabindex="0" |
| 49 | + > |
| 50 | + Select File |
| 51 | + </span> |
| 52 | + </label> |
| 53 | + </span> |
| 54 | + </div> |
| 55 | +</div> |
| 56 | +``` |
| 57 | + |
| 58 | +**Note:** Since the component applies a class of `sr-only` (hides the input) to the input itself, the input must have an id assigned to it |
| 59 | +for the label to be able to trigger a click on the input. By default, the component assigns the `id` to the `name` attribute if you don't |
| 60 | +provide an `id` to it. |
| 61 | + |
| 62 | +## Upload Progress |
| 63 | + |
| 64 | +By default, if you add a `wire:model` to the component, it will hook into livewire's file uploads and display upload progress when |
| 65 | +a file is selected. A progress bar indicating upload progress is shown once an upload has been started. |
| 66 | + |
| 67 | +If you would like to not show progress, or to handle the display of the upload progress yourself, you may pass in a false value |
| 68 | +for the `display-upload-progress` attribute. |
| 69 | + |
| 70 | +```html |
| 71 | +<x-file-upload wire:model="avatar" :display-upload-progress="false" /> |
| 72 | +``` |
| 73 | + |
| 74 | +**Note:** Since the upload progress hooks into livewire events, it will not be shown unless you provide a `wire:model` to it. |
| 75 | + |
| 76 | +## Custom Button Label |
| 77 | + |
| 78 | +By default, the text on the button that is shown says `Select File`. You may optionally specify your own label via an attribute: |
| 79 | + |
| 80 | +```html |
| 81 | +<x-file-upload name="avatar" label="Choose New Avatar" /> |
| 82 | +``` |
| 83 | + |
| 84 | +## Default Slot |
| 85 | + |
| 86 | +The `file-upload` component is based on the photo input example from TailwindUI. This displays a photo to the left of the button. |
| 87 | +This slot is completely optional, and can be omitted if you don't need to show a file preview. |
| 88 | + |
| 89 | +If you are using livewire and would like to show a photo here, you can do so by following this example: |
| 90 | + |
| 91 | +```html |
| 92 | +<x-file-upload name="avatar" wire:model="avatar"> |
| 93 | + <div> |
| 94 | + @if ($avatar) |
| 95 | + <span class="block w-20 h-20"> |
| 96 | + <img class="rounded-full w-full" src="{{ $avatar->temporaryUrl() }}" /> |
| 97 | + </span> |
| 98 | + @endif |
| 99 | + </div> |
| 100 | +</x-file-upload> |
| 101 | +``` |
| 102 | + |
| 103 | +## After Slot |
| 104 | + |
| 105 | +You can of course omit the default slot and provide content in the `after` slot to show a file preview on the right side of the button. |
| 106 | +Other content could also be shown in this slot as well. |
| 107 | + |
| 108 | +```html |
| 109 | +<x-file-upload name="avatar"> |
| 110 | + <x-slot name="after"> |
| 111 | + <div>After slot content.</div> |
| 112 | + </x-slot> |
| 113 | +</x-file-upload> |
| 114 | +``` |
| 115 | + |
| 116 | +## Multiple Files |
| 117 | + |
| 118 | +You can use the component to upload multiple files by providing the `multiple` attribute to the component. If you're using livewire and `wire:model`, just make |
| 119 | +sure you're model is an array to handle the uploads. |
0 commit comments