File picker (type="file")
HTML Living Standard
Allow users to choose local files. An upload form usually submits with POST and multipart/form-data.
On this page
Attributes and behavior
| Value or syntax | What it does |
|---|---|
accept=".pdf,image/*" | Suggests file types in the chooser. Validate the file’s type and size on the server. |
multiple | Allows choosing more than one file. |
required | An empty field prevents a normal validated submission. |
Try the example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>File picker</title>
<style>
form { display: grid; gap: 12px; justify-items: start; } output { display: block; margin-top: 10px; }
</style>
</head>
<body>
<form id="demo">
<label>Handouts <input type="file" name="handouts" accept=".pdf,image/*" multiple required></label>
<button type="submit">Check</button>
</form>
<output id="response"></output>
<script>
const form = document.querySelector('#demo');
form.addEventListener('click', event => {
const submitter = event.target.closest('[type="submit"], [type="image"]');
if (!submitter) return;
event.preventDefault();
if (!form.noValidate && !submitter.formNoValidate && !form.reportValidity()) return;
const values = new FormData(form, submitter);
document.querySelector('#response').textContent =
'Passed validation. Fields received: ' + [...values.keys()].join(', ');
});
</script>
</body>
</html>
What to expect Open the chooser. The demo keeps any selected files in the browser.