Add FeaturesIntermediate

Add File Uploads to Your App

Implement secure file uploads in your Next.js app using presigned URLs and cloud storage. Covers direct browser uploads to S3 or Supabase Storage, file validation, and serving uploaded files.

Upload Architecture: Direct vs. Server-Side

The recommended pattern is direct browser-to-storage uploads using presigned URLs - this avoids routing large files through your server and reduces bandwidth costs. Your server generates a short-lived signed URL, the browser uploads directly to S3 or Supabase Storage, and then notifies your server when complete. Server-side uploads are simpler but bottleneck your API and can hit serverless function payload limits. For files larger than a few megabytes, always use direct uploads.

Setting Up Supabase Storage

Create a storage bucket in the Supabase dashboard and configure public or private access based on your needs. Install `@supabase/supabase-js` and call `supabase.storage.from('bucket').upload(path, file)` from the browser to upload directly. For private files, generate a signed URL with `createSignedUrl` when you need to display them. Set bucket policies to restrict file types and sizes to prevent abuse.

Presigned Uploads to AWS S3

Install `@aws-sdk/s3-request-presigner` and create an API route that calls `getSignedUrl` with a `PutObjectCommand`. Set an expiry of 60 seconds and restrict the allowed content type to the expected file format. Return the presigned URL and the final file URL to the client. In the browser, PUT the file directly to the presigned URL using `fetch` with the file as the body and the matching `Content-Type` header.

Validating Files Before Upload

Validate file type and size on the client before requesting a presigned URL to give instant feedback. Check `file.type` against an allowlist of MIME types and enforce a maximum size in megabytes. Re-validate on the server when generating the presigned URL by inspecting the requested content type and rejecting anything outside your allowlist. Never trust client-side validation alone - always enforce limits server-side.

Displaying Upload Progress

Use `XMLHttpRequest` instead of `fetch` for presigned URL uploads if you need progress events - `xhr.upload.onprogress` fires with `loaded` and `total` byte counts. Compute the percentage and update a progress bar in state. Alternatively, use a library like `uppy` or `react-dropzone` with a tus or S3 multipart plugin for resumable uploads with built-in progress UI. Show a thumbnail preview immediately after the user selects a file using `URL.createObjectURL`.

Serving and Transforming Files

For images, use a CDN or image transformation service like Cloudflare Images or Imgix to serve optimized WebP versions at the right dimensions. Store only the base file path in your database and construct CDN URLs at render time. For private documents, always generate short-lived signed URLs rather than exposing the raw storage URL. Set appropriate Cache-Control headers on public assets to maximize CDN hit rates.

Need help with this?

Our team handles add features for AI-built apps every day. Get a fixed quote within 24 hours.

Start with a self-serve audit

Get a professional review of your app at a fixed price.

Security Review

Automated Security Scan

$19

AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.

Get Started
Most Popular

Security Review

Manual Security Review

from $250

Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.

Get a Quote

Security Review

Full Pentest

Custom

Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.

Fix Bugs

Code Audit

$19

AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.

Get Started
Most Popular

Fix Bugs

Bug Fixing

from $200

Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.

Get a Quote

Fix Bugs

Ongoing Support

Custom

Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.

Refactor Code

Code Audit

$19

AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.

Get Started
Most Popular

Refactor Code

Refactoring

from $400

Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.

Get a Quote

Refactor Code

Full Rewrite

Custom

Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.

100% of your audit purchase is credited toward any paid service. Start with an audit, then let us fix what we find.

Related technologies

Need help with your app?

Tell us about your project. We'll respond within 24 hours with a clear plan and fixed quote.

Tell Us About Your App