Skip to content
Guides
Navigation
Feedback
Overlays
Layout
Data Display
Forms
.md

Forms

file-upload

Drag-and-drop dropzone over a native file input, with a file list, image previews and client-side type and size checks.

    Installation

    php artisan ui:add file-upload

    Usage

    {{-- Several files, checked against accept and max-size in the browser --}}
    <x-ui.file-upload name="attachments[]" multiple accept=".png,.jpg,.pdf" max-size="10MB" />
    
    {{-- One image; a new pick replaces the old one --}}
    <x-ui.file-upload name="avatar" accept="image/*" max-size="2MB" />
    
    {{-- Error state from server validation, custom copy --}}
    <x-ui.file-upload name="contract" accept=".pdf" :invalid="$errors->has('contract')"
        prompt="Drop the signed contract here" hint="PDF only" />
    
    {{-- Livewire (WithFileUploads): wire:model lands on the native input --}}
    <x-ui.file-upload wire:model="photos" multiple accept="image/*" />
    
    <script>
        // The input fires a normal change event after every drop or removal.
        const field = document.querySelector('input[name="attachments[]"]');
        field.addEventListener('change', () => console.log(field.files.length));
    
        // Uploading is up to you, for example:
        // fetch('/upload', { method: 'POST', body: new FormData(field.form) });
    </script>

    The native file input stays the submitted field: name, wire:model, id and required pass through to it, and it fires change after every drop or removal. accept and max-size (bytes or 10MB) are checked in the browser; rejected files are listed with an error and never added. It does not upload: send new FormData(form) with fetch, or use Livewire WithFileUploads.

    Props

    Every prop with its default. Pass any of them as attributes, using : to bind a PHP value.

    @props([
        'multiple' => false,
        'accept' => null,
        'maxSize' => null,
        'disabled' => false,
        'invalid' => false,
        'prompt' => 'Drag files here or click to browse',
        'hint' => null,
        'upTo' => 'up to :size',
        'removeLabel' => 'Remove :name',
        'typeError' => 'This file type is not allowed.',
        'sizeError' => 'File is larger than :size.',
    ])