Lesson 6 of 9
File Uploads & FormData
File uploads use <input type='file'> and require special handling. The form must use enctype='multipart/form-data' for file uploads to work. You can restrict accepted file types, allow multiple files, and even access the device camera on mobile. Understanding file uploads is essential for building profile picture uploaders, document submission forms, and media upload features.
File Upload Attributes
- accept='image/*' - Only allow image files (JPEG, PNG, WebP, etc.).
- accept='.pdf,.doc,.docx' - Only allow specific file extensions.
- accept='video/*' - Only allow video files.
- multiple - Allow selecting multiple files at once.
- capture='user' - Open front camera on mobile devices.
- capture='environment' - Open back camera on mobile devices.
- CRITICAL: The <form> MUST have enctype='multipart/form-data' for file uploads to work. Without it, only the filename is sent, not the file data.
File Upload Example
File Upload Example
<!DOCTYPE html>
<html>
<body>
<h2>File Upload Form</h2>
<form enctype="multipart/form-data" method="POST" action="/upload">
<!-- Single image upload -->
<label for="avatar">Profile Photo:</label>
<input type="file" id="avatar" name="avatar" accept="image/*" required>
<!-- Multiple document upload -->
<label for="docs">Documents (PDF only):</label>
<input type="file" id="docs" name="docs" accept=".pdf" multiple>
<!-- Mobile camera capture -->
<label for="selfie">Take Selfie:</label>
<input type="file" id="selfie" name="selfie" accept="image/*" capture="user">
<button type="submit">Upload Files</button>
</form>
</body>
</html>Diagram
Loading diagram…
File uploads require multipart/form-data encoding to transfer binary data
Key Takeaways
- File uploads use <input type='file'> and require special handling.
- accept='image/*' - Only allow image files (JPEG, PNG, WebP, etc.).
- accept='.pdf,.doc,.docx' - Only allow specific file extensions.
- accept='video/*' - Only allow video files.
Finished this lesson?
0 of 9 in Module 6