1. components
  2. file upload
  3. svelte

File Upload

Allow upload of files with buttons or drag and drop.

Select file or drag here

Select file or drag here

Attach up to 2 files.

Button

Use the default children slot to overwrite the default dropzone interface. Allow for a custom interface.

Disabled

Select file or drag here

Bind to API

You can optionally bind to the internal Zag component API to access additional methods such as clearFiles().

Select file or drag here

RTL

Select file or drag here

Additional Features

Accepted File Formats

<FileUpload accept="image/*">
<FileUpload accept={"text/html": [".html", ".htm"]}>

File Validation

<FileUpload maxFiles={5}>
<FileUpload minFileSize={1024 * 1024 * 5}> <!-- 5 mb -->
<FileUpload maxFileSize={1024 * 1024 * 10}> <!-- 10 mb -->

Custom Validation

function validateFileSize(file) {
if (file.size > 1024 * 1024 * 10) return ["FILE_TOO_LARGE"];
return null;
}
<FileUpload validate={validateFileSize} onFileReject={console.error}>

Events

<FileUpload onFileChange={console.log}> <!-- Triggers when files changed. -->
<FileUpload onFileAccept={console.log}> <!-- Triggers when files are accepted. -->
<FileUpload onFileReject={console.error}> <!-- Triggers when files are rejected. -->

Image Previews

function generatePreview(event) {
const reader = new FileReader();
reader.onload = (event) => {
const image = event.target.result;
// set the image as the src of an image element
}
reader.readAsDataURL(details.acceptedFiles[0]);
}
<FileUpload onFileChange={generatePreview}>

Miscellaneous

<FileUpload allowDrop> <!-- Enable drag-and-drop -->
<FileUpload directory> <!-- Enable dirctories -->
<FileUpload capture> <!-- Enable media capture on mobile devices -->