Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

File Upload for UI SDK

A drop zone and browse button that collect files, check their type, size and count, and list them before upload. For React on UI SDK.

On this page9 sections

A file upload is a drop zone and a browse button that collect files in the browser and list them, each with its own remove button. It sends nothing by itself: your code uploads the accepted files, or a form submits them through the hidden file input. Reach for it for an avatar, a receipt or a batch of documents on a form.

Once a file is on its way to the server, an Attachment shows it as a chip with the state of its upload.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

File Upload anatomy: root, dropzone, trigger, item, item preview, item name, item delete trigger

Usage

import { Button } from "@/components/ui/button";
import {
  FileUpload, FileUploadDescription, FileUploadDropzone,
  FileUploadDropzoneIcon, FileUploadList, FileUploadTitle,
  FileUploadTrigger,
} from "@/components/ui/file-upload";
const FileUploadDemo = () => (
  <FileUpload className="w-full max-w-sm">
    <FileUploadDropzone>
      <FileUploadDropzoneIcon />
      <FileUploadTitle>Upload files</FileUploadTitle>
      <FileUploadDescription>
        Drag and drop or click to browse. PNG, JPG, PDF up to 10MB.
      </FileUploadDescription>
      <FileUploadTrigger render={<Button size="sm" variant="outline" />}>
        Browse files
      </FileUploadTrigger>
    </FileUploadDropzone>
    <FileUploadList />
  </FileUpload>
);

Accessibility

The dropzone takes dropped and pasted files. With no FileUploadTrigger inside it, it is also a button in the tab order that opens the file dialog on Enter or Space. With a trigger inside, the trigger is the tab stop. A FileUploadTitle inside the dropzone names it. Without one, set ids.label on FileUpload to a visible label's id, or ids.hiddenInput to the id a FieldLabel's htmlFor points at, so pressing the label opens the dialog too. The file input is hidden from screen readers. The list of files is one tab stop, and each remove button, such as Remove report.pdf, stays out of the tab order. UI SDK announces files added, rejected or removed through a polite live region.

Enter
When focus is on the dropzone or a trigger, opens the file dialog. On the clear button, empties the list.
Space
When focus is on the dropzone or a trigger, opens the file dialog. On the clear button, empties the list.
Arrow Down
When focus is on a file in the list, moves focus to the next file, stopping at the last.
Arrow Up
When focus is on a file in the list, moves focus to the previous file, stopping at the first.
Arrow Right
When focus is on a file in the list, moves focus to the next file, or to the previous one when the list reads right to left.
Arrow Left
When focus is on a file in the list, moves focus to the previous file, or to the next one when the list reads right to left.
Home
When focus is on a file in the list, moves focus to the first file.
End
When focus is on a file in the list, moves focus to the last file.
Backspace
When focus is on a file in the list, removes it and moves focus to the next file, or to the previous one when it was the last.
Delete
When focus is on a file in the list, removes it and moves focus to the next file, or to the previous one when it was the last.
Tab
Moves focus to the dropzone when it has no trigger inside and no disableClick, then to the trigger, the clear button and the list of files, which takes one stop.
Shift+Tab
Moves focus to the previous focusable element.

Demos

Accepted file types

accept is image/png,image/jpeg, so the dialog offers PNG and JPEG files, and a dropped file of any other type is rejected and never reaches the list. The helper line under the title names the formats, because a rejected file disappears here without a message. Set accept to what your server takes, and read the reasons in onFileReject to show them, as Rejected files does.

Preview width

Clear trigger

A trash button in the top corner is FileUploadClearTrigger: pressing it empties the list, and it stays hidden while there is nothing to clear. maxFiles is 5 here, so a batch can build up, and one press drops all of it. Add a clear button wherever people may want to start their pick over rather than remove files one at a time.

Preview width

Custom preview

Up to 8 images are accepted, and they show as a four-column grid of thumbnails, each with a round remove button on its corner, in place of FileUploadList. The grid reads acceptedFiles from useFileUploadContext and renders each file through FileUploadItem. Build a photo picker or a gallery upload on this.

Preview width

Custom spacing

The dropzone sets a --space variable to spacing step 4, and step 6 from the medium breakpoint, but nothing in FileUploadDropzone reads that variable, so this zone looks the same as the one in Dropzone. Its inset comes from px-6 and py-8 in the component. Pass a padding class such as p-4 to FileUploadDropzone to change it.

Preview width

Directory upload

directory on FileUpload sets webkitdirectory on the hidden input, so the Select Folder button opens a folder dialog and hands over every file inside, listed under the button. maxFiles counts those files, so it is 100 here, and from a folder holding more, only the first 100 are added. Use it for a project folder or a set of scans.

Preview width

Dropzone

The dashed box is the whole control: an icon and a title over the list, with no separate button, because a click on the box or Enter on it opens the file dialog. Use it when a second button would repeat that action. Add FileUploadTrigger for a labeled button, or set disableClick on FileUploadDropzone when the box should only take drops.

Preview width

Media capture

capture is environment, which asks a phone to open its rear camera for the Take a picture button rather than the photo library, and user asks for the front one. Use it for a step that needs a new photo, such as a receipt or a damaged parcel. A browser with no camera to offer opens its usual file dialog.

Preview width

Multiple files

maxFiles is 5, and the helper line says so, since the limit is invisible until someone passes it. With the default of 1, a new file replaces the one already chosen. A pick or a drop that would go past 5 is trimmed: the files that fit are added, and the rest are rejected as too many.

Preview width

Rejected files

PNG and JPEG images up to 1 MB are accepted, three at most, and every file turned away is listed below the accepted ones with its reason, such as larger than 1 MB, read from the error codes onFileReject receives. Without a list like this, a refused file never appears and nothing says why. Show reasons whenever accept, maxFileSize or maxFiles can refuse something.

Preview width

Trigger

There is no dropzone: FileUploadTrigger renders as an outline button with a paperclip, and the chosen file is listed under it. Use a bare trigger beside a comment box or a chat input, where a large dashed area would take over the form. With no FileUploadDropzone on the page, nothing takes a drop, so the button is the only way in.

Preview width

With field

The upload sits in a Field under the label Upload images, and the dropzone holds only its icon, since the label already says what goes there. With no title inside it, the dropzone points aria-labelledby at that label, so a screen reader announces it as Upload images. Use this layout when the upload is one question in a longer form, lined up with the other fields.

Preview width

FAQ

When should I use File Upload instead of an Attachment?

Use File Upload to choose files: it collects them in the browser, checks them against your limits and lists them until your code sends them. An Attachment shows one file as a chip with its name and the state of your upload, such as uploading or failed, but it can't pick anything.

The two work together in a message composer or a support form. A bare Trigger picks the files, and each one becomes an attachment chip once its upload starts.

Does File Upload send files to a server?

No, it only collects them. To send them with an ordinary form post, give FileUpload a name: its hidden file input holds the accepted files, so they go with the form as long as the form is encoded as multipart/form-data.

For anything else, such as an upload with a progress bar, read the accepted files in onFileAccept and send them yourself in a FormData body. File Upload keeps no upload state of its own, so show each upload's progress beside it, or its state with an Attachment.

How do I get the files a File Upload accepted?

onFileAccept on FileUpload receives the whole accepted list in details.files each time it changes, removals included, and onFileChange receives both lists, acceptedFiles and rejectedFiles. Inside the root, useFileUploadContext returns the same state, which is how the Custom preview demo builds its grid.

The values are the browser's own File objects, so they go straight into a FormData body. Pass defaultAcceptedFiles to start with files already in the list.

How do I limit the file types and sizes a File Upload accepts in React?

Set accept to MIME types or extensions, such as image/* or .pdf: the dialog filters by it, and a dropped file of another type is rejected. maxFileSize and minFileSize are in bytes, so 5 MB is 5242880, and maxFiles caps the count at 1 unless you raise it.

For a rule of your own, pass validate. It runs on each file that passed accept and the size limits, and returns null when the file is fine or an array of error codes when it isn't. The first of those codes reaches onFileReject with the file.

Why doesn't my file show up in the File Upload list?

It was rejected, and FileUploadList shows accepted files only. A file is turned away when its type isn't in accept, its size falls outside minFileSize and maxFileSize, your validate returns errors, or a file with the same name, size and type is already listed. When a pick would take the list past maxFiles, the files that don't fit are rejected.

onFileReject receives each rejected file with its error codes, such as "FILE_TOO_LARGE" or "TOO_MANY_FILES", so you can show a message, as the Rejected files demo does. With maxFiles at its default of 1, a new file replaces the old one instead.

Can File Upload accept a whole folder or open the phone camera?

Both. directory on FileUpload makes the dialog pick a folder and adds every file inside it, and a folder dropped on the dropzone is read the same way, subfolders included. Raise maxFiles to cover the folder's contents, as the Directory upload demo does, because the files past the limit are rejected.

capture set to environment or user asks a phone to open its rear or front camera instead of the photo library, as in Media capture. A browser with no camera to offer opens its usual file dialog.

How do I remove a file from a File Upload?

Each row in FileUploadList has its own remove button, and FileUploadClearTrigger empties the list in one press, as the Clear trigger demo shows. In a list of your own, put FileUploadItemDeleteTrigger inside each FileUploadItem, as Custom preview does.

From code, useFileUploadContext inside the root gives you deleteFile for one file and clearFiles for all of them. A removed file also leaves the hidden input, so it won't go with the form.

How do I style a File Upload while a file is dragged over it?

Style data-dragging, which FileUpload and FileUploadDropzone both carry while files are held over the dropzone. The component already uses it: the dashed border turns to the primary color over a faint tint, and FileUploadDropzoneIcon picks up the same color through the root's group/file-upload class.

Change those classes in components/ui/file-upload, or add data-dragging variants to the classes you pass the dropzone. data-invalid on the dropzone follows invalid and draws the border in the error color.

API Reference

FileUpload

<div> data-slot="file-input"

PropType
accept?Accept

The files to accept: MIME types, wildcards and extensions, as a string, a list or a map of types to extensions.

acceptedFiles?File[]

The files, to control it.

announcement?(change: FileInputChange) → string | null

What screen readers hear after a change. Return null for nothing.

Type
(change: FileInputChange) → string | null
Default
"2 files accepted. 1 file rejected.", or "photo.png removed."
Parameters

change – FileInputChange

Returns
string | null
capture?"environment" | "user" | boolean

Opens the camera on phones instead of the files: "user" for the front camera, "environment" for the back one, or true for the default one.

Type
"environment" | "user" | boolean
controller?FileInputController

Adds and removes files from anywhere. Create one with createFileInputController().

debug?boolean

Logs every state change with its reason to the console, in development builds.

defaultAcceptedFiles?File[]

The files at first, when uncontrolled.

directory?boolean

Picks a whole folder, with every file inside it. Turns on multiple.

disabled?boolean

Disables it like a native control: the trigger can't be focused or used, drops and pastes are ignored, and nothing is submitted.

form?string

The id of a form to belong to, when it isn't inside that form.

ids?object

The ids of its root, dropzone, trigger and native file input, and of the element that labels it.

Type
{ dropzone?: string; hiddenInput?: string; label?: string; root?: string; trigger?: string }
invalid?boolean

Marks it invalid, with data-invalid on the root, the dropzone and the trigger.

Default
false
locale?string

The locale file sizes are written in.

Default
"en-US"
maxFiles?number

The most files it holds. Above 1, new files join the ones it has.

Default
1
maxFileSize?number

The largest size accepted, in bytes.

Default
Infinity
minFileSize?number

The smallest size accepted, in bytes.

Default
0
name?string

The name the files submit under. Without one, nothing is submitted.

onFileAccept?(details: { files: File[] }) → void

Runs when the files change, with every file it holds.

Type
(details: { files: File[] }) → void
Parameters

details – { files: File[] }

Returns
void
onFileChange?(details: { acceptedFiles: File[]; rejectedFiles: FileRejection[]; }) → void

Function called when the value changes, whether accepted or rejected.

Type
(details: { acceptedFiles: File[]; rejectedFiles: FileRejection[]; }) → void
Parameters

details – { acceptedFiles: File[]; rejectedFiles: FileRejection[]; }

Returns
void
onFileReject?(details: { files: FileRejection[] }) → void

Runs when the files a pick, drop or paste left out change, with their reasons.

Type
(details: { files: FileRejection[] }) → void
Parameters

details – { files: FileRejection[] }

Returns
void
required?boolean

Blocks form submission until it holds a file, with the browser's own message.

translations?object

The localized messages to use.

Type
{ deleteFile?: (file: File) → string; dropzone?: string; itemPreview?: (file: File) → string }
validate?( file: File, details: { acceptedFiles: File[]; rejectedFiles: FileRejection[] } ) → FileError[] | null

A check of your own: return reasons to reject the file, or null to accept it.

Type
( file: File, details: { acceptedFiles: File[]; rejectedFiles: FileRejection[] } ) → FileError[] | null
Parameters

file – File

details – { acceptedFiles: File[]; rejectedFiles: FileRejection[] }

Returns
FileError[] | null
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-dragging?string

Present while files are dragged over one of its drop zones.

data-required?string

Present when a file is required.

FileUploadRootProvider

PropType
valueFileInputController

What useFileUpload() returns: the state and API, made outside the component to control it.

accept?Accept

The files to accept: MIME types, wildcards and extensions, as a string, a list or a map of types to extensions.

acceptedFiles?File[]

The files, to control it.

announcement?(change: FileInputChange) → string | null

What screen readers hear after a change. Return null for nothing.

Type
(change: FileInputChange) → string | null
Default
"2 files accepted. 1 file rejected.", or "photo.png removed."
Parameters

change – FileInputChange

Returns
string | null
capture?"environment" | "user" | boolean

Opens the camera on phones instead of the files: "user" for the front camera, "environment" for the back one, or true for the default one.

Type
"environment" | "user" | boolean
debug?boolean

Logs every state change with its reason to the console, in development builds.

defaultAcceptedFiles?File[]

The files at first, when uncontrolled.

directory?boolean

Picks a whole folder, with every file inside it. Turns on multiple.

disabled?boolean

Disables it like a native control: the trigger can't be focused or used, drops and pastes are ignored, and nothing is submitted.

form?string

The id of a form to belong to, when it isn't inside that form.

ids?object

The ids of its root, dropzone, trigger and native file input, and of the element that labels it.

Type
{ dropzone?: string; hiddenInput?: string; label?: string; root?: string; trigger?: string }
invalid?boolean

Marks it invalid, with data-invalid on the root, the dropzone and the trigger.

locale?string

The locale file sizes are written in.

maxFiles?number

The most files it holds. Above 1, new files join the ones it has.

Default
1
maxFileSize?number

The largest size accepted, in bytes.

minFileSize?number

The smallest size accepted, in bytes.

name?string

The name the files submit under. Without one, nothing is submitted.

onFileAccept?(details: { files: File[] }) → void

Runs when the files change, with every file it holds.

Type
(details: { files: File[] }) → void
Parameters

details – { files: File[] }

Returns
void
onFileChange?(details: { acceptedFiles: File[]; rejectedFiles: FileRejection[]; }) → void
Type
(details: { acceptedFiles: File[]; rejectedFiles: FileRejection[]; }) → void
Parameters

details – { acceptedFiles: File[]; rejectedFiles: FileRejection[]; }

Returns
void
onFileReject?(details: { files: FileRejection[] }) → void

Runs when the files a pick, drop or paste left out change, with their reasons.

Type
(details: { files: FileRejection[] }) → void
Parameters

details – { files: FileRejection[] }

Returns
void
required?boolean

Blocks form submission until it holds a file, with the browser's own message.

translations?object
Type
{ deleteFile?: (file: File) → string; dropzone?: string; itemPreview?: (file: File) → string }
validate?( file: File, details: { acceptedFiles: File[]; rejectedFiles: FileRejection[] } ) → FileError[] | null

A check of your own: return reasons to reject the file, or null to accept it.

Type
( file: File, details: { acceptedFiles: File[]; rejectedFiles: FileRejection[] } ) → FileError[] | null
Parameters

file – File

details – { acceptedFiles: File[]; rejectedFiles: FileRejection[] }

Returns
FileError[] | null
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadTrigger

<button> data-slot="file-input-trigger"

PropType
controller?FileInputController

Connects a trigger outside the file input, like one in a toolbar, to the file input with this controller.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-required?string

Present when a file is required.

FileUploadDropzone

<div> data-slot="file-input-dropzone"

PropType
disableClick?boolean

Keeps a press on the zone from opening the file picker.

Default
false
pasteTarget?FileInputPasteTarget

Somewhere else to take pasted files from, on top of pastes while focus is inside the zone: "document", an element, a CSS selector, or a function that returns an element, like a prompt's text area. Text pastes are left alone.

target?FileInputDropzoneTarget

Where it takes drops. "document" takes files dropped anywhere on the page, and the zone shows data-dragging while files are over the window, so it can be a full-page overlay.

Default
"element"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-drag-accept?string

Present while dragged files look acceptable, from their types and count.

data-drag-reject?string

Present while a dragged file has a type accept rules out, or there are too many.

data-dragging?string

Present while files are dragged over the zone, or the window with target="document".

FileUploadDropzoneIcon

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadTitle

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadDescription

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadHelper

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadItemGroup

PropType
type?ItemType

Which files it lists: accepted or rejected.

Default
"accepted"
className?string

Merged with the part's classes.

FileUploadItem

<div> data-slot="file-input-item"

PropType
fileFile

The file it's for.

type?ItemType

A rejected file, from rejectedFiles, isn't in the list's keyboard order.

Default
"accepted"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

FileUploadItemPreview

PropType
type?string

Shows only for files whose type matches this pattern, like image/*. * * @default ".*".

Default
".*"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadItemPreviewImage

<img> data-slot="file-upload-item-preview-image"

PropType
className?string

Merged with the part's classes.

AttributeType
data-type?string

Mirrors type.

FileUploadItemName

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadItemSize

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

FileUploadItemDeleteTrigger

<button> data-slot="file-input-item-remove"

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

FileUploadClearTrigger

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)

FileUploadList

PropType
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

useFileUpload