skip to content

In HTML, what does the accept attribute on <input type="file"> actually do, and what does adding multiple change?

level: seniorimportance: should knowfreq 38%

answer

  1. a picker filter, not a gate
  2. dot-prefixed extensions, or MIME types
  3. three wildcard groups only
  4. the second selection replaces, not appends
  5. the extension is not the bytes

basics

~20 s

accept filters what the operating system's file picker shows by default, using MIME types, wildcards like image/* or extensions like .pdf. It is a convenience hint the user can override, not validation, so the file that arrives can be anything. multiple lets one field carry several files.

solid answer

~50 s

`accept` is a hint to the file picker, nothing more. You give it a comma-separated list of MIME types (`application/pdf`), wildcard groups (`image/*`, `audio/*`, `video/*`) or extensions written with a dot (`.csv`), and the browser asks the OS dialog to preselect that filter. Users can usually flip the dialog back to "All files", drag-and-drop paths often bypass the filter, and a file's extension says nothing about its bytes — so `accept` never guarantees what you receive, and it produces no validity error you can check. `multiple` allows the field to hold more than one selected file rather than replacing the previous choice, which matters because a second selection normally overwrites the first rather than appending. Two related attributes are worth naming: `capture` with the value `user` or `environment` asks a mobile device to open the front or rear camera instead of the file browser, and the control's value cannot be set programmatically to a path — a browser will only let script clear it.

code

html · 10 lines
html
<label for="docs">Supporting documents (PDF or CSV)</label>
<input
  id="docs"
  name="docs"
  type="file"
  accept=".pdf,application/pdf,.csv,text/csv"
  multiple>

<label for="receipt">Photo of receipt</label>
<input id="receipt" name="receipt" type="file" accept="image/*" capture="environment">

go deeper

for a junior

Know the syntax: accept takes MIME types, the image/audio/video wildcards, or dot-prefixed extensions, and multiple lets one field hold several files.

for a middle

Explain why accept is a picker hint rather than a constraint — overridable dialogs, drag-and-drop, extension-derived types — and that it produces no validity error to check.

for a senior

Demonstrate that you have built an uploader: replacement rather than append on reselection, no size attribute, no settable value, and type determined from content where the user cannot interfere.

for a principal

Own the upload contract end to end — which types are permitted, where that decision is enforced, how limits are communicated, and how one shared uploader keeps every team on the same rules.

## What accept is for `<input type="file" accept="...">` communicates *intent* to the file picker. When the user opens the dialog, the browser passes the list along to the platform, and the dialog defaults to showing matching files. The accepted syntax is a comma-separated list of three kinds of token: ```html <!-- MIME types --> <input type="file" accept="application/pdf,image/png"> <!-- wildcard groups: the three defined ones --> <input type="file" accept="image/*"> <!-- file extensions, each starting with a dot --> <input type="file" accept=".csv,.tsv"> <!-- mixed, which is the practical form --> <input type="file" accept=".pdf,application/pdf"> ``` The syntax details matter and are a fair interview probe. An extension must begin with a dot — `accept="pdf"` is not an extension token and `accept="*.pdf"` is not one either. Only `audio/*`, `video/*` and `image/*` are defined wildcard groups; there is no `document/*`. Listing both an extension and a MIME type is common because platforms differ in which one they match well. ## Why it is not validation This is the point the question is really testing. 1. **The user can override the filter.** Most native dialogs offer an "All files" option, and the field still accepts what the user then chooses. 2. **Drag-and-drop can bypass it.** Dropping a file onto the control does not go through the dialog's filter in the same way. 3. **The type is guessed anyway.** The browser reports a file's type largely from its extension and OS association. Renaming `payload.bin` to `photo.png` makes the picker show it and makes it report an image type. 4. **There is no validity flag for it.** A file whose type does not match `accept` does not turn the control invalid in any way you can query — it is not part of the constraint machinery at all. So the correct mental model is: `accept` improves the odds that the user finds the right file quickly. It states nothing about the bytes. Whatever inspects the upload has to determine the real type from the content itself, and that inspection belongs somewhere the user cannot reach. ## What multiple changes ```html <label for="shots">Screenshots</label> <input id="shots" name="shots" type="file" accept="image/*" multiple> ``` Without `multiple` the control holds exactly one file, and every new selection replaces the previous one. With `multiple` the dialog permits a multi-selection and the control holds the whole set. The behaviour that surprises teams is that a *second* trip to the dialog still **replaces** the current selection rather than adding to it. "Add more files" — the pattern every upload UI wants — is therefore not something the element gives you; the page has to remember the earlier selection itself and present the combined list. Candidates who have shipped an uploader volunteer this immediately. ## capture `capture` is the mobile-specific companion: ```html <input type="file" accept="image/*" capture="environment"> ``` The value `environment` asks for the outward-facing camera and `user` for the front-facing one. It is a request, not a guarantee: a desktop browser ignores it, and a device may still present a chooser. Note that `capture` narrows the flow to capturing new media, which is wrong for a field where the user might legitimately want an existing photo. ## The value is not settable A file input's value cannot be assigned a path by script. Browsers permit clearing it only. This is a long-standing platform rule: a page must never be able to nominate a file from the user's disk without an explicit user gesture. It is worth knowing because it explains why "pre-fill the upload field with the file they chose last time" is not implementable in HTML. ## Labelling and appearance The file control renders as a platform button plus a filename readout, and its appearance is deliberately hard to change — again because the browser must keep the user's choice honest. The usual pattern is to associate a visible label with the input and style around the control rather than replacing it, keeping the real input in the accessibility tree so it stays keyboard-operable. ## The senior framing In one sentence: `accept` and `capture` shape the *choosing* experience, `multiple` shapes what the control can *hold*, and none of the three tells you anything reliable about what actually arrives. A candidate who says "accept restricts uploads to images" has described the demo; one who says "accept preselects a filter and I still determine the type from the content elsewhere" has described production.

  • A product manager asks you to block files over 10 MB. Is there an attribute for that?
    No — there is no size attribute on a file input, and `min`/`max` do not apply to the file state. The page can inspect the chosen files' reported sizes and tell the user before uploading, which is good feedback, but that check runs in a place the user controls. Anything that actually enforces a limit has to live where the upload is received.
  • Your uploader needs an "add more files" flow. Why does multiple not give you that?
    Because a fresh trip to the picker replaces the control's current selection rather than appending to it. The element models "the files chosen right now", not a growing basket. The page keeps its own list, adds each new selection to it, renders that list with remove controls, and submits from its own state — the input becomes the picker, not the store.
  • Why can't you set a file input's value from script to pre-fill last time's document?
    Browsers forbid assigning a path to a file control; only clearing it is allowed. If a page could nominate files, any site could read arbitrary paths from the user's disk without a deliberate choice. The consequence for design is that "reuse your previous upload" must be a server-side reference to an already-uploaded file, not a pre-populated field.

saying these in an interview costs you the question

  • Says accept prevents the wrong file type from being uploaded
  • Writes accept="pdf" or accept="*.pdf" instead of ".pdf"
  • Assumes a reported MIME type reflects the file's actual bytes
  • Expects a second selection to append to the current files
  • Thinks capture guarantees the camera opens on every device

context