File upload

A styled file field with drag-and-drop and local image previews.

File upload

Needs Alpine. See getting started.

It does not upload anything. It's a real <input type="file">, so a plain form POST and wire:model both work unchanged. See where the line is.

Use it

<form method="POST" action="/attachments" enctype="multipart/form-data">
    @csrf
    <x-ds::file-upload
        name="attachments[]"
        label="Attachments"
        :multiple="true"
        accept="image/*,.pdf"
        :maxSize="5 * 1024 * 1024"
    />
    <x-ds::button type="submit">Upload</x-ds::button>
</form>

enctype="multipart/form-data" is yours to remember — without it the browser posts only filenames, and the failure looks like a broken upload rather than a missing attribute.

Props

PropTypeDefaultWhat it does
namestringrequiredField name. Add [] yourself when multiple.
labelstring—
multipleboolfalseAccept more than one file.
acceptstring—"image/*", ".pdf,.csv". Rendered as a human hint.
maxSizeint—Bytes. Checked in the browser only.
previewbooltrueThumbnail for image files, read locally.
helpstring—
errorstring—
disabledboolfalse
filesarray—Rows for files the HOST knows about: see Progress, from the host.
listLabelstringUploadsThe rows' list's name.
summaryLabelstring:done of :total uploadedOver the rows.

With Livewire

<x-ds::file-upload name="photo" label="Profile photo" accept="image/*" wire:model="photo" />

Livewire owns the upload; the component just presents the field.

Progress, from the host

The field still uploads nothing, but it can show what YOUR upload is doing: one row per file, with a bar, the outcome, and Remove, Retry and Cancel. Give it files, or file-upload-items in the slot:

<x-ds::file-upload name="docs[]" label="Documents" :multiple="true" :files="[
    ['id' => 1, 'name' => 'report.pdf', 'size' => 482000, 'status' => 'uploading', 'progress' => 40],
    ['id' => 2, 'name' => 'q3.csv', 'status' => 'failed', 'error' => 'Over 10 MB.'],
    ['id' => 3, 'name' => 'logo.png', 'status' => 'done', 'href' => '/files/3', 'thumbnail' => '/files/3/thumb'],
]" />

File upload rows

Row keyWhat it does
idYours, sent back with every event.
nameRequired.
sizeBytes.
progress0–100 while uploading; null draws an indeterminate bar.
statusqueued, uploading, done or failed.
errorWhy it failed.
hrefThe uploaded file: the name links to it once done.
thumbnailAn image URL.
removablefalse hides Remove.

As a file-upload-item these are props, with removeLabel, retryLabel, cancelLabel and statusText ([status => words]).

The buttons dispatch, bubbling, with { id, name } as the detail: Cancel (cancel-upload) on a queued or uploading file, Retry (retry) on a failed one, Remove (remove) on a finished or failed one. They need Alpine and the package's script (getting started).

Why cancel-upload and not cancel: a browser fires its own cancel on the file input when the reader closes the file dialog without choosing, and it bubbles through the same ancestors. Before 30B the button's event was cancel too, and a listener had to check for a detail.

End to end with Livewire

Livewire uploads a wire:model file field itself and announces it on the input: livewire-upload-start, -progress (with detail.progress), -finish, -error and -cancel. Given files (even an empty array), the field listens for those and draws the progress on the files just chosen; when the upload finishes they give way to your rows:

<div x-on:remove="$wire.removeUpload('docs', $event.detail.id)"
     x-on:cancel-upload="$wire.cancelUpload('docs')">
    <x-ds::file-upload name="docs" label="Documents" :multiple="true" wire:model="docs"
        :files="collect($docs)->map(fn ($file) => [
            'id' => $file->getFilename(),
            'name' => $file->getClientOriginalName(),
            'size' => $file->getSize(),
            'status' => 'done',
            'thumbnail' => $file->isPreviewable() ? $file->temporaryUrl() : null,
        ])->all()" />
</div>

Livewire sends every file chosen in one request, so the files just chosen share one figure. Your own uploader (chunks, a queue, S3 direct) can report per file instead: keep each row's status and progress on the server and re-render.

What is said

Each row's bar is named by the file ("report.pdf") and carries its value; the percentage on screen is hidden from screen readers, so it is never read as it changes. A row's outcome is said once, politely, when it turns done or failed ("q3.csv: Failed. Over 10 MB."). The tally over the rows, "2 of 3 uploaded", is plain text.

With validation

<x-ds::file-upload
    name="contract"
    label="Signed contract"
    accept=".pdf"
    :maxSize="10 * 1024 * 1024"
    :error="$errors->first('contract')"
/>

accept and maxSize are courtesies, not validation. Both are hints to the file dialog and both can be bypassed by dragging. Validate on the server:

$request->validate(['contract' => ['required', 'mimes:pdf', 'max:10240']]);

What it does not do

No transport. Uploading needs somewhere to upload to, and a component that knows that has a backend contract — which nothing else in this system has. Livewire solves it with wire:model; a plain form solves it with <form>. The progress rows above draw what your uploader reports; the package makes no request of its own.

Don't

  • Don't forget [] on the name when multiple. PHP keeps only the last file, and nothing reports it.
  • Don't rely on accept or maxSize for validation.
  • Don't use it for an avatar that needs cropping. That's a different component and it doesn't exist.

More in specs/file-upload.md.


From the package repo: edit this page on GitHub.

Was this page helpful?