File Upload

A component that is used to upload multiple files.

Loading...

Anatomy

<FileUpload.Root>
  <FileUpload.Label />
  <FileUpload.Dropzone>
    <FileUpload.Trigger />
  </FileUpload.Dropzone>
  <FileUpload.ItemGroup>
    <FileUpload.Item>
      <FileUpload.ItemPreview>
        <FileUpload.ItemPreviewImage />
      </FileUpload.ItemPreview>
      <FileUpload.ItemName />
      <FileUpload.ItemSizeText />
      <FileUpload.ItemDeleteTrigger />
    </FileUpload.Item>
  </FileUpload.ItemGroup>
  <FileUpload.ClearTrigger />
  <FileUpload.HiddenInput />
</FileUpload.Root>

Examples

Initial Files

Use defaultAcceptedFiles to set the initial files.

Clear Trigger

Use ClearTrigger to remove all files at once.

Dropzone

Use Dropzone for drag-and-drop. Style it with data-dragging.

Directory Upload

Set directory to upload whole folders. Read file paths from file.webkitRelativePath.

For directories with many files, raise or remove maxFiles to avoid rejections.

Accepted File Types

Use accept to restrict file types by MIME type (image/png) or extension (.pdf).

Error Handling

Set constraints with maxFiles, maxFileSize, minFileSize, and accept. Rejected files include error codes like TOO_MANY_FILES, FILE_INVALID_TYPE, FILE_TOO_LARGE, or FILE_EXISTS.

File Transformations

Use transformFiles to process files before they're added, for example to compress, convert, or resize images.

Field

Use Field to add helper text and error handling.

Root Provider

Use RootProvider with the useFileUpload hook to access the state and methods from outside the component.

Pasting Files

Use setClipboardFiles to accept images pasted from the clipboard.

Media Capture

Use capture to open the device camera: "environment" for the back camera, "user" for the front.

Rejected Files

Read rejectedFiles from the context to show validation errors.

Guides

File Previews

Set type on ItemPreview to render a preview per file format.

  • type="image/*": image thumbnails via ItemPreviewImage
  • type="video/*": video files
  • type="application/pdf": PDF files
  • type=".*": fallback for any file
<FileUpload.ItemPreview type="image/*">
  <FileUpload.ItemPreviewImage />
</FileUpload.ItemPreview>

<FileUpload.ItemPreview type="video/*">
  <VideoIcon />
</FileUpload.ItemPreview>

<FileUpload.ItemPreview type="application/pdf">
  <PdfIcon />
</FileUpload.ItemPreview>

<FileUpload.ItemPreview type=".*">
  <FileIcon />
</FileUpload.ItemPreview>

Disable Dropzone

Set allowDrop={false} to disable drag-and-drop.

<FileUpload.Root allowDrop={false}>{/* ... */}</FileUpload.Root>

Prevent Document Drop

Dropping files outside the dropzone doesn't navigate away by default. Set preventDocumentDrop={false} to turn this off.

<FileUpload.Root preventDocumentDrop={false}>{/* ... */}</FileUpload.Root>

Prevent Double Open

Set disableClick on Dropzone when it contains a Trigger, so the file picker doesn't open twice.

<FileUpload.Dropzone disableClick>
  <FileUpload.Trigger>Choose Files</FileUpload.Trigger>
  Drag files here
</FileUpload.Dropzone>

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
accept
Record<string, string[]> | FileMimeType | FileMimeType[]

The accept file types

acceptedFiles
File[]

The controlled accepted files

allowDroptrue
boolean

Whether to allow drag and drop in the dropzone element

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
capture
'user' | 'environment'

The default camera to use when capturing media

defaultAcceptedFiles
File[]

The default accepted files when rendered. Use when you don't need to control the accepted files of the input.

directory
boolean

Whether to accept directories, only works in webkit browsers

disabled
boolean

Whether the file input is disabled

ids
ElementIds

The ids of the elements. Useful for composition.

invalid
boolean

Whether the file input is invalid

locale'en-US'
string

The current locale. Based on the BCP 47 definition.

maxFiles1
number

The maximum number of files

maxFileSizeInfinity
number

The maximum file size in bytes

minFileSize0
number

The minimum file size in bytes

name
string

The name of the underlying file input

onFileAccept
(details: FileAcceptDetails) => void

Function called when the file is accepted

onFileChange
(details: FileChangeDetails) => void

Function called when the value changes, whether accepted or rejected

onFileReject
(details: FileRejectDetails) => void

Function called when the file is rejected

preventDocumentDroptrue
boolean

Whether to prevent the drop event on the document

readOnly
boolean

Whether the file input is read-only

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadRootState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

required
boolean

Whether the file input is required

transformFiles
(files: File[]) => Promise<File[]>

Function to transform the accepted files to apply transformations

translations
IntlTranslations

The localized messages to use.

validate
(file: File, details: FileValidateDetails) => FileError[] | null

Function to validate a file

AttributeDescription
[data-file-upload-root]<uid>
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-dragging]Present when in the dragging state

ClearTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-clear-trigger]<uid>
[data-disabled]Present when disabled
[data-readonly]Present when read-only

Dropzone

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
disableClick
boolean

Whether to disable the click event on the dropzone

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadDropzoneState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-dropzone]<uid>
[data-invalid]Present when invalid
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-dragging]Present when in the dragging state

HiddenInput

Defines the <input> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

ItemDeleteTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-item-delete-trigger]<uid>
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-type]The type of the item

ItemGroup

Defines the <ul> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadItemGroupState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

type
ItemType

AttributeDescription
[data-file-upload-item-group]<uid>
[data-disabled]Present when disabled
[data-type]The type of the item

ItemName

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-item-name]<uid>
[data-disabled]Present when disabled
[data-type]The type of the item

ItemPreviewImage

Defines the <img> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-item-preview-image]<uid>
[data-disabled]Present when disabled
[data-type]The type of the item

ItemPreview

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

type'.*'
string

The file type to match against. Matches all file types by default.

AttributeDescription
[data-file-upload-item-preview]<uid>
[data-disabled]Present when disabled
[data-type]The type of the item

Item

Defines the <li> element.

AttributeDefaultType
file
File

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadItemState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-item]<uid>
[data-disabled]Present when disabled
[data-type]The type of the item

ItemSizeText

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-item-size-text]<uid>
[data-disabled]Present when disabled
[data-type]The type of the item

Label

Defines the <label> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-label]<uid>
[data-disabled]Present when disabled
[data-invalid]Present when invalid
[data-required]Present when required

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseFileUploadReturn

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
type ONLY_FOR_FORMAT = ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadRootProviderState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Trigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<FileUploadTriggerState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-file-upload-trigger]<uid>
[data-disabled]Present when disabled
[data-readonly]Present when read-only
[data-invalid]Present when invalid

Context

API

PropertyType
dragging
boolean

Whether the user is dragging something over the root element

focused
boolean

Whether the user is focused on the dropzone element

disabled
boolean

Whether the file input is disabled

readOnly
boolean

Whether the file input is in read-only mode

transforming
boolean

Whether files are currently being transformed via `transformFiles`

maxFilesReached
boolean

Whether the maximum number of files has been reached

remainingFiles
number

The number of files that can still be added

openFilePicker
VoidFunction

Function to open the file dialog

deleteFile
(file: File, type?: ItemType | undefined) => void

Function to delete the file from the list

acceptedFiles
File[]

The accepted files that have been dropped or selected

rejectedFiles
FileRejection[]

The files that have been rejected

setFiles
(files: File[]) => void

Sets the accepted files

clearFiles
VoidFunction

Clears the accepted files

clearRejectedFiles
VoidFunction

Clears the rejected files

getFileSize
(file: File) => string

Returns the formatted file size (e.g. 1.2MB)

createFileUrl
(file: File, cb: (url: string) => void) => VoidFunction

Returns the preview url of a file. Returns a function to revoke the url.

setClipboardFiles
(dt: DataTransfer | null) => boolean

Sets the clipboard files Returns `true` if the clipboard data contains files, `false` otherwise.

getRootState
() => RootState

Returns the state of the root

getDropzoneState
() => DropzoneState

Returns the state of the dropzone

getTriggerState
() => TriggerState

Returns the state of the trigger

getItemGroupState
(props: ItemGroupProps) => ItemGroupState

Returns the state of the item group

getItemState
(props: ItemProps) => ItemState

Returns the state of the item