Set up file storage
Choose a storage backend for uploaded files. For local development, use filesystem storage:For production, use S3 or S3-compatible storage:
app/storage.ts
app/storage.ts
Create an upload handler
Build a function that processes uploaded files and returns a public URL:The upload handler receives a
app/utils/uploads.ts
FileUpload object with the file data and metadata.Configure form data middleware
Add the form data middleware with your upload handler:The middleware automatically parses
app/router.ts
multipart/form-data requests and processes file uploads.Create an upload form
Build a form with file input fields:Note the
app/pages/upload.tsx
enctype="multipart/form-data" attribute - this is required for file uploads.Handle file uploads
Process uploaded files in your route handler:After the middleware processes the upload, the file field contains the URL string returned by your upload handler.
app/router.ts
Serve uploaded files
Create a route to serve files from storage:The
app/router.ts
createFileResponse function handles content negotiation, range requests, and caching headers automatically.Validate file uploads
Add validation to check file size, type, and other constraints:Handle validation errors in your route:
app/utils/uploads.ts
Handle multiple file uploads
Support uploading multiple files at once:Process multiple files:
app/pages/gallery-upload.tsx