--- title: "file-upload" description: "Drag-and-drop dropzone over a native file input, with a file list, image previews and client-side type and size checks." category: "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 ```bash php artisan ui:add file-upload ``` ## Dependencies - composer: `gehrisandro/tailwind-merge-laravel` ## Usage ```blade {{-- Several files, checked against accept and max-size in the browser --}} {{-- One image; a new pick replaces the old one --}} {{-- Error state from server validation, custom copy --}} {{-- Livewire (WithFileUploads): wire:model lands on the native input --}} ``` ## Example The demo rendered on the docs page. ```blade
``` ## Source Copy this file to the path shown above it. `resources/views/components/ui/file-upload.blade.php` ```blade @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.', ]) @php // max-size takes bytes or a string like "500KB" / "10MB" (1024-based). $maxBytes = null; if ($maxSize !== null && preg_match('/^\s*([\d.]+)\s*(B|KB|MB|GB)?\s*$/i', (string) $maxSize, $m)) { $power = ['B' => 0, 'KB' => 1, 'MB' => 2, 'GB' => 3][strtoupper($m[2] ?? '') ?: 'B']; $maxBytes = (int) round((float) $m[1] * 1024 ** $power); } // Default hint: "PNG, JPG, PDF up to 10MB", built from accept and max-size. if ($hint === null) { $types = $accept ? implode(', ', array_map(fn ($t) => strtoupper(ltrim(trim($t), '.')), explode(',', $accept))) : ''; $limit = $maxSize !== null ? str_replace(':size', $maxSize, $upTo) : ''; $hint = trim($types . ' ' . $limit); } $classes = \TailwindMerge\Laravel\Facades\TailwindMerge::merge( 'group/file-upload grid w-full gap-3', $attributes->get('class'), ); $zone = $invalid ? 'border-destructive group-data-[state=dragging]/file-upload:border-destructive' : 'border-input group-data-[state=dragging]/file-upload:border-primary'; @endphp
    @once @endonce ``` ## Theme tokens `--input`, `--primary`, `--accent`, `--muted`, `--muted-foreground`, `--destructive`, `--ring`, `--radius` 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`.