shadcn.io is an independent project built on shadcn/ui
Components
Free
File Upload for Vue UI SDK
A drop zone and browse button that collect files, check their type, size and count, and list them before upload. For Vue 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.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
<script setup lang="ts">
import { Button } from"@/components/ui/button"import {
FileUpload, FileUploadDescription, FileUploadDropzone,
FileUploadDropzoneIcon, FileUploadList, FileUploadTitle,
FileUploadTrigger,
} from"@/components/ui/file-upload"
</script>
<template>
<FileUploadclass="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>
<template #render="{ props }">
<Buttonv-bind="props"size="sm"variant="outline">Browse files</Button>
</template>
</FileUploadTrigger>
</FileUploadDropzone>
<FileUploadList />
</FileUpload>
</template>
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, point its aria-labelledby at a visible label, as the With field demo does. 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. deleteFile in translations rewords that name, and UI SDK announces files added, rejected or removed through a polite live region.
KeyAction
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 a @file-accept handler 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?
Listen for @file-accept, which receives the whole accepted list in details.files each time it changes, removals included, or @file-change, which 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 Vue?
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.
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
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?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?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?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?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?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
FileUploadTitle
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
FileUploadDescription
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
FileUploadHelper
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
FileUploadItemGroup
PropType
type?ItemType
Which files it lists: accepted or rejected.
Default
"accepted"
class?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?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?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?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
FileUploadItemPreviewImage
<img>data-slot="file-upload-item-preview-image"
PropType
class?string
Merged with the part's classes.
AttributeType
data-type?string
Mirrors type.
FileUploadItemName
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
FileUploadItemSize
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
FileUploadItemDeleteTrigger
<button>data-slot="file-input-item-remove"
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when disabled, by its own prop or a <fieldset>.
FileUploadClearTrigger
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
FileUploadList
PropType
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.