@softr/sdk
file-uploads
>-
Boundary. upload lives on @softr/sdk/server and runs inside server functions. useFileUpload and FileItem live on @softr/sdk and run in components. FileAttachment comes from @softr/sdk and can be imported anywhere.
File uploads
useFileUpload handles the full upload cycle — XHR with real progress, per-upload cancel, and state management. It uses a server function the SDK registers itself, so no route file is needed in the app.
Setup
import { defineMutation, type FileAttachment, useFileUpload, type FileItem } from '@softr/sdk'
import { ds } from '@softr/sdk/server'Core pattern
const createApplication = defineMutation({
id: 'createApplication',
datasourceId: '8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77',
operation: 'create',
fields: { name: 'V1StGXR8', resume: 'Z7pQm4Kd' },
permissions: { userGroupTypes: ['LOGGED_IN_USERS'] },
})
const submitApplication = createServerFn({ method: 'POST' })
.validator((data: { name: string; resume: FileAttachment[] }) => data)
.handler(async ({ data }) => {
await ds.create(createApplication, { data })
})
function ApplicationForm() {
const { files, isUploading, upload, remove } = useFileUpload(createApplication)
return (
<form onSubmit={async (e) => {
e.preventDefault()
if (isUploading) return
const resume = files.filter(f => f.status === 'done') as FileAttachment[]
await submitApplication({ data: { name: e.currentTarget.name.value, resume } })
}}>
<input name="name" />
<input type="file" onChange={(e) => { const f = e.target.files?.[0]; if (f) upload(f) }} />
{files.map((f) => (
<div key={f.id}>
{f.filename}
{f.status === 'uploading' && <> — {f.progress}% <button type="button" onClick={f.cancel}>Cancel</button></>}
{f.status === 'done' && <button type="button" onClick={() => remove(f.id)}>Remove</button>}
{f.status === 'error' && <span> — Failed: {f.error.message}</span>}
</div>
))}
<button type="submit" disabled={isUploading}>Submit</button>
</form>
)
}useFileUpload takes the same DatasourceAction you pass to ds.create / ds.update — no separate upload action to declare.
What FileAttachment looks like
type FileAttachment = {
url: string // clean URL, no internal query params
filename: string // original filename
id: string // file id extracted from the upload response
type?: string | null // content type from the upload response, or null when the platform doesn't know it
}Pass attachments as the field value in ds.create / ds.update.
FileItem shape
files is a single ordered array. Each entry is a discriminated union on status:
type FileItem =
| { status: 'uploading'; id: string; filename: string; progress: number; cancel: () => void }
| { status: 'done'; id: string; filename: string; url: string } // FileAttachment + status
| { status: 'error'; id: string; filename: string; error: Error }Items appear in insertion order and transition in place — a completed upload stays at its original position rather than moving to a separate list.
For form submission, filter to done items: files.filter(f => f.status === 'done') as FileAttachment[].
Blocking form submission
isUploading is true while any item has status: 'uploading'. Disable the submit button and guard the submit handler.
Edit forms: pre-loading existing attachments
const { files, isUploading, upload, remove } = useFileUpload(
updateApplication,
{ initialAttachments: record.fields.resume ?? [] },
)Pre-loaded attachments appear as status: 'done' items at the start of files. The existing attachments already have the FileAttachment shape from the datasource record.
Server-side uploads and plain forms
For server-side use (no progress tracking needed) or plain HTML forms, use upload directly:
import { upload, ds } from '@softr/sdk/server'
const submitResume = createServerFn({ method: 'POST' })
.validator((fields: FormData) => fields)
.handler(async ({ data }) => {
const uploaded = await upload(createApplication, data.get('cv') as File)
await ds.create(createApplication, {
data: { fullName: data.get('fullName') as string, cv: [uploaded] },
})
return new Response(null, { status: 303, headers: { Location: '/thank-you' } })
})
function ApplyForm() {
return (
<form action={submitResume.url} method="POST">
<input type="text" name="fullName" />
<input type="file" name="cv" />
<button type="submit">Apply for this role</button>
</form>
)
}Failure modes
Submitting while isUploading is true. Only done items have a url and should be submitted. In-progress uploads finish after the record is saved and are never included. Always filter to status === 'done' before passing to ds.create / ds.update.
Not passing initialAttachments on an edit form. The hook starts with an empty list. Without pre-loading, the first save erases existing attachments.
References
- record-mutations/SKILL.md: creating and updating records with the collected attachments
- current-user/SKILL.md: gating uploads to the signed-in user