← Back to HTML & CSS, page 6

html-css-0106 · Medium mid

How does a file input work, and how do you read the chosen files in the browser?

A file input gives a FileList of File objects after the user chooses, and the change event tells you. accept is only a hint for the picker, so check type and size in script and on the server. Forms that upload files need method post and enctype multipart/form-data.

Full answer

A file input is <input type="file">. Useful attributes:

  • accept suggests which files the picker shows, for example accept="image/png, image/jpeg" or accept=".pdf". It is a hint for the picker only. A user can still choose another kind of file, so check the type and size in a script and on the server.
  • multiple allows more than one file.
  • capture asks a phone to use the camera for some types. Support differs, so test it.
  • webkitdirectory lets a user pick a folder in browsers that support it.

After the user picks files, the input's files property is a FileList. Each item is a File with name, size, type and lastModified. The change event fires when the choice changes. A file can be read in the page with await file.text() or file.arrayBuffer(). To show a preview of an image, use URL.createObjectURL(file) and revoke the address when done.

A form that sends files must use method="post" and enctype="multipart/form-data". Without that encoding, only the file names are sent.

For a nicer look, the native control is hard to style. A common safe pattern is a label styled as a button that is connected to a visually hidden input. Keep the input focusable so the keyboard still works.

The script lists the number of files, counts the ones over 1000 bytes and reads the first one as text. A real page should show a message in an element for screen readers and handle large files with care.

The native picker window cannot be driven by this check, so the example sets files through a script instead.

html
<form id="f" method="post" enctype="multipart/form-data">
  <label for="pic">Choose pictures</label>
  <input type="file" id="pic" name="pics" accept="image/png, image/jpeg" multiple>
  <output id="out"></output>
</form>
<script>
  pic.addEventListener("change", async () => {
    const files = [...pic.files];
    const tooBig = files.filter(f => f.size > 1000);
    const first = files.length ? await files[0].text() : "";
    out.textContent = files.length + " file(s), " + tooBig.length + " too big, first: " + first;
  });
</script>

The check set the files by script, because the native file picker window cannot be driven here. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • files is a FileList of File objects with name, size and type.
  • accept and capture are hints, not rules.
  • multiple allows several files.
  • Use multipart/form-data to send file contents.
  • Keep a visually hidden input focusable if you restyle it.

Common mistakes

  • Trusting accept to block other file types.
  • Forgetting enctype="multipart/form-data", so the server gets file names and not file contents.
  • Hiding the input with display: none so keyboard users cannot reach it.

Follow-ups

  • How do you show an image preview before upload?
  • What is the difference between accept and a size check?

Tips

  • Say that accept is a hint, and the server must check.
  • Mention enctype. It is a classic bug.