File Uploads with Actions
Learn to handle file uploads directly through Server Actions, including secure storage and processing.
Uploading Files with Actions
File uploads are a common feature, allowing users to share images, documents, and more. Traditionally, handling these required complex API routes and separate handlers.
With Next.js Server Actions, handling file uploads becomes much simpler and more integrated directly within your forms, keeping your logic co-located.
Setting Up the File Input
To allow file uploads, you need an HTML form with a specific setup. The <input type="file"> element is essential for selecting files from a user's device.
- Use the
nameattribute to identify the file in your Server Action. - Add
enctype="multipart/form-data"to your<form>tag. This tells the browser to encode the form data, including files, correctly.
<form action="/api/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="myFile" />
<button type="submit">Upload</button>
</form>All lessons in this course
- Optimistic UI Updates
- File Uploads with Actions
- Validation & Error Handling in Server Actions