Guides
A React file upload component that everyone can use
For developers adding attachments or imports to a React app. A keyboard- and screen-reader-friendly uploader with progress, cancel and retry, plugged into presigned URLs or your own endpoint.
The uploader
Attachments
Progress is simulated in this demo. A file named “fail.pdf” fails so you can try Retry.
Attachments
Drag files here or
PDF, images or spreadsheets, up to 10 MB each. Name a file “fail…” to try Retry.
import { FileUploader } from '@reactloom/files';
import { demoUpload } from './demo-adapter';
export default function Example() {
return (
<div style={{ width: '100%', maxWidth: 520 }}>
<FileUploader
label="Attachments"
hint="PDF, images or spreadsheets, up to 10 MB each. Name a file “fail…” to try Retry."
accept="image/*,.pdf,.xlsx,.csv"
maxSize={10_000_000}
upload={demoUpload}
/>
</div>
);
}Accessible first, drag and drop second
- A real
<input type="file">sits behind a “Browse files” button, so keyboards, screen readers, phone pickers and camera capture all work. Dropping files is an extra, not the only way. - Files that are added, rejected (wrong type, too big, too many), uploaded or failed are announced politely, and rejections are also written out, because a toast that disappears is easy to miss.
- Every action names its file — “Cancel upload of report.pdf” — so a list of buttons still makes sense out of context.
Bring your own transport
FileUploader takes an upload function: it receives the file, an AbortSignal (Cancel) and an onProgress callback, and returns a promise. The package handles the queue, concurrency, retries and status; you decide where bytes go.
import type { UploadAdapter } from '@reactloom/files';
/** Ask your API for a presigned URL, then PUT the file straight to storage (S3, GCS, R2). */
export const presignedUpload: UploadAdapter = async (file, { signal, onProgress }) => {
const res = await fetch('/api/uploads', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ name: file.name, type: file.type, size: file.size }),
signal,
});
if (!res.ok) throw new Error('Could not start the upload');
const { url, key } = await res.json();
// XMLHttpRequest, because fetch cannot report upload progress yet.
await new Promise<void>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open('PUT', url);
xhr.setRequestHeader('content-type', file.type);
xhr.upload.onprogress = (e) => e.lengthComputable && onProgress(e.loaded / e.total);
xhr.onload = () => (xhr.status < 300 ? resolve() : reject(new Error(`Storage returned ${xhr.status}`)));
xhr.onerror = () => reject(new Error('Network error'));
signal.addEventListener('abort', () => xhr.abort());
xhr.send(file);
});
return { key };
};Collect the results with onStatusChange and submit the storage keys with your form, rather than the files themselves.
Limits and checks
accept,maxSize,minSizeandmaxFilescheck files before upload and explain why one was not added. State the limits inhintbefore people choose files.- Client checks are for feedback only. Check type (by content, not just extension) and size again on the server, and scan uploads if your users share files with each other.