Skip to main content

Component | File Drop

Render a file drop zone that allows users to upload files from the browser to your app.

import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

function onFiles(files: { buffer: Buffer, name: string, type: string }[]) {
files.forEach(file => {
const filePath = path.join(__dirname, file.name);

// write to disk
fs.writeFileSync(filePath, file.buffer);
});
}

ui.fileDrop("pdf-file-drop", {
label: "Drop files here",
acceptedFileTypes: ["application/pdf"],
onChange: onFiles
})

Learn more about file uploads.

API reference

Function signature

type ComposeFile = {
buffer: Buffer;
name: string;
type: string;
}

ui.fileDrop(
id: string,
properties: Partial<{
label: string,
description: string,
required: bool,
validate: (value: ComposeFile[]) => string | void,
onChange: (value: ComposeFile[]) => void,
acceptedFileTypes: string[],
minCount: int,
maxCount: int,
style: Style | null
}>
)

Parameters

id

required string
A unique identifier for the component. This is necessary so that Compose can properly pass user actions back to the component.

properties.label

optional string

The label to display above the file drop. If not provided, the label will be inferred from the id.


properties.description

optional string

A description to display between the label and file drop.


properties.required

optional boolean

Validate that the user has dropped at least one file before submitting the form it's part of or calling it's onChange hook. Defaults to true.


properties.validate

optional (files: ComposeFile[]) => string | void

Validate the file drop value before submitting it. If the function returns a string, it will be displayed as an error message.

The validator will be passed a list of files. Each file is an object with the following properties:

{
buffer: Buffer;
name: string;
type: string;
}

properties.onChange

optional (files: ComposeFile[]) => void

A callback function that is called when the user changes the value of the file drop.

The callback will be passed a list of files. Each file is an object with the following properties:

{
buffer: Buffer;
name: string;
type: string;
}

properties.acceptedFileTypes

optional string[]

A list of file types that the user can upload. Defaults to all file types.

The file types should be MIME type strings. For example, image/png or application/pdf. Learn more about MIME types here.


properties.minCount

optional number

Validate that the user has dropped at least some number of files. Defaults to 0.


properties.maxCount

optional number

Validate that the user has dropped at most some number of files. Defaults to 1000000000.


properties.style

optional Style

Directly style the underlying element using CSS. See the styling guide for more details on available style properties.