HTML file upload fields allow users to select files from their devices and submit them through a web form. They are commonly used for uploading profile pictures, resumes, documents, certificates, and other attachments. HTML provides the <input type="file"> element to create a file upload field.
What Is an HTML File Upload?
An HTML file upload field allows users to choose one or more files from their computer or mobile device. Depending on the browser and device, users may see a file picker or another file-selection interface.
For example, a job application website can use a file upload field to let candidates attach their resumes.
To understand how form controls work, explore HTML Input and HTML Input Types.
HTML File Upload Example
The following example demonstrates how to create a basic file upload form.
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="resume">Upload Your Resume:</label>
<input
type="file"
id="resume"
name="resume"
required
>
<button type="submit">Upload File</button>
</form>
In this example:
<input type="file">creates the file selection field.id="resume"identifies the input.name="resume"specifies the field name sent with the form data.requiredmakes selecting a file mandatory.method="post"sends the form data using an HTTP POST request.enctype="multipart/form-data"enables the form to transmit uploaded file data.
The /upload address is an example endpoint. A real website needs a server-side upload handler to receive and process the file.
Allowing Specific File Types
The accept attribute can suggest which file types users should select. This is useful when a website accepts only certain documents or images.
<label for="document">Upload a Document:</label>
<input
type="file"
id="document"
name="document"
accept=".pdf,.doc,.docx"
>
This example suggests PDF and Microsoft Word document formats.
You can also allow image files:
<input type="file" name="profile-picture" accept="image/*">
The accept attribute is a selection hint, not a security measure. A server must verify the actual file type and content.
Allowing Multiple File Uploads
The multiple attribute allows users to select more than one file in the same field.
<label for="photos">Upload Photos:</label>
<input
type="file"
id="photos"
name="photos"
accept="image/*"
multiple
>
Users can select multiple images, depending on their device and browser. The server must also support receiving multiple files.
For related form options, learn about HTML Form Attributes and HTML Form Elements.
Important File Upload Attributes
HTML file upload fields support several useful attributes.
acceptsuggests which file types are suitable.multipleallows multiple file selections.requiredrequires a file to be selected.nameidentifies the uploaded field in form data.disabledprevents users from interacting with the field.
These attributes improve the user experience, but they do not replace server-side upload validation.
File Upload Security Best Practices
File uploads require careful security handling. Follow these practices when building an upload feature:
- Validate file size and permitted file types on the server.
- Do not trust the filename or extension alone.
- Check file contents where appropriate.
- Store uploaded files securely and restrict access when necessary.
- Use HTTPS to protect data during transmission.
- Apply suitable upload limits to reduce abuse and resource consumption.
An HTML form alone cannot securely store or process uploaded files. The backend must handle file reception, validation, and storage.
Frequently Asked Questions
Can HTML upload files without a backend?
HTML can display a file picker, but uploading files to a website normally requires a server or an upload service that receives the data.
What does enctype="multipart/form-data" do?
It allows the browser to encode form data in a format suitable for transmitting files alongside other form fields.
Can I restrict uploads to PDF files?
Yes. You can use accept=".pdf" to suggest PDF files. However, the server must independently validate uploaded files.
How can users upload multiple files?
Add the multiple attribute to the file input. The receiving server must be configured to handle multiple files.