Directory
- Welcome
File Upload Component
The File Upload component provides a robust, user-friendly interface for uploading files. It supports drag-and-drop, multiple file selection, progress tracking, and comprehensive file validation.
Key Features
-
Drag & Drop: Intuitive file dropping interface
-
Progress Tracking: Real-time upload progress visualization
-
File Validation: Type and size restrictions
-
Multiple Layouts: Table and card view options
Basic Usage
Drag and Drop Files Here or Click to Upload
Accepted File Types: .jpg, .png, .pdf
Max File Size: 5MB
<FileUpload id="basic-upload" label="Upload Files" accept={['.jpg', '.png', '.pdf']} maxSize={5}/>
Layout Variants
Choose between two display styles for uploaded files, drag a file into the upload area’s below to see it in action. Your files will not be uploaded, this is a demo:
Table Layout (Default)
Drag and Drop Files Here or Click to Upload
Accepted File Types: .jpg, .png
Max File Size: 10MB
Card Layout
Drag and Drop Files Here or Click to Upload
Accepted File Types: .jpg, .png
Max File Size: 10MB
States
Disabled State
Drag and Drop Files Here or Click to Upload
Accepted File Types: .jpg, .png
Max File Size: 5MB
Error State
Drag and Drop Files Here or Click to Upload
Accepted File Types: .jpg, .png
Max File Size: 5MB
Upload Component Props API
Prop | Type | Default | Description |
---|---|---|---|
accept | string | string[] | * | Allowed file types (e.g., [".jpg", ".pdf"]) |
actionLabel | string | Select Files | Text for the upload button |
disabled | boolean | false | Disable the upload functionality |
label | string | undefined | Label text above the upload area |
labelProps | HTMLAttributes<"label"> | undefined | Additional label element props |
maxSize | number | 10 | Maximum file size in MB |
maxFiles | number | undefined | Maximum number of files to upload |
multiple | boolean | false | Allow multiple file uploads |
variant | table | card | table | Layout style for uploaded files |
Upload Functions API
The component provides several functions to manage the upload lifecycle.
Function | Type | Description |
---|---|---|
updateProgress | ({ name: string, percent: number }) => void | Update the progress of a file by its name (0 - 100) |
set: uploadFileHandler | ({ file: File }) => Promise<{ url?: string; error?: string }> | Define a function to handle file uploads |
set: removeFileHandler | ({ fileName: string }) => void | Define a function to handle file removals |
set: validateFileHandler | ({ file: File }) => boolean | Define a function to handle custom file validations |
get: fileList | Map<string, { url?: string; error?: string }> | Get a list of uploaded files |
File Validation
-
File Type: Validates against
accept
prop -
File Size: Enforces
maxSize
limit -
Multiple Files: Respects
multiple
prop setting -
Max Files: Respects
maxFiles
limit -
Custom Validation: Supports additional validation through handlers
Custom Validation
The component supports additional validation through handlers. You can set the validateFileHandler
prop to a function that returns a boolean value indicating whether the file is valid.
Uploading The Files
This component doesn’t handle the file upload, it only provides a UI for the user to upload files. You need to handle the file upload in your own code defined in the uploadFileHandler
function.
Limited Time Launch Sale
Start building accessible file upload experiences today with our AstroJS starter template.
GET 60% OFF!