Skip to main content

File Uploads

Learn how to handle file uploads in React Router applications using actions and forms.

Overview

React Router handles file uploads through the standard FormData API. Files are submitted to action functions where you can process, validate, and store them.

Basic File Upload

Handle a single file upload:

Multiple File Uploads

Handle multiple files at once:

Upload with Progress

Show upload progress using a fetcher:

Client-Side Preview

Show image preview before uploading:

Direct Upload to Cloud Storage

Upload directly to services like S3 or Cloudflare R2:

Signed Upload URLs

Use presigned URLs for direct client-to-storage uploads:

Best Practices

  1. Always validate files - Check type, size, and content on the server
  2. Set size limits - Prevent large uploads from overwhelming your server
  3. Use encType=“multipart/form-data” - Required for file uploads
  4. Sanitize filenames - Remove dangerous characters before storage
  5. Consider direct uploads - Upload to cloud storage directly to reduce server load
  6. Show progress - Provide feedback for large uploads
  7. Handle errors gracefully - Network issues and file problems should be recoverable