Guides

HTML file input on phones and desktops, accept and capture

What accept, capture and multiple really do in each browser, how iPhone HEIC photos arrive, and how to validate and fill a file input.

By BeamFieldPublished 8 min read
View as Markdown

<input type="file"> is a file dialog on a computer and a camera and photo menu on a phone. Here is what its attributes really do in each browser, and where they stop helping.

What the file input gives you for free

<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="photo">Photo of the receipt</label>
  <input id="photo" name="photo" type="file" required>
  <button>Send</button>
</form>

With no JavaScript, this markup gives you:

  • The operating system's picker. The page never sees the file system: input.value only returns the file name prefixed with C:\fakepath\.
  • A File object per file in input.files: a Blob with a name, a size in bytes, a type and a lastModified timestamp.
  • Native submission through enctype="multipart/form-data", with required support.
  • Events: input and change after a selection, cancel when the picker is closed without a new choice.

To send the file yourself, put it in a FormData:

const input = document.querySelector('#photo');

input.addEventListener('change', async () => {
  const [file] = input.files;
  if (!file) return;

  const body = new FormData();
  body.append('photo', file);
  await fetch('/upload', { method: 'POST', body });
});

The MDN reference for the file input documents all of this. One trap: file.type is deduced from the extension, not from the bytes, so a PNG renamed to .txt reports text/plain.

accept is a hint for the picker, not validation

accept takes a comma-separated list of tokens. The HTML standard allows three kinds: a MIME type without parameters, one of the wildcards image/*, video/* and audio/*, and a file extension starting with a dot.

<input type="file" accept="image/*">
<input type="file" accept="image/jpeg,image/png,.pdf">
<input type="file" accept=".docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document">

The standard encourages giving both the MIME type and the extension when you want one specific format, as in the third line.

accept only changes the picker. On a computer, files that do not match are usually dimmed, but MDN notes that the user can in most cases switch the dialog to all files. On a phone, image/* is what makes the camera appear among the choices. Support is uneven too: caniuse lists accept as partial in Chrome for Android and Safari on iOS, and as unsupported in Firefox for Android.

So accept improves the experience and never guarantees the type. Files that arrive by drag and drop, paste or script skip it, and a JavaScript check on file.type only checks the extension. Real validation happens on the server.

capture goes straight to the camera, and nowhere else

capture comes from the W3C HTML Media Capture Recommendation. It has two keywords, user for the front camera and environment for the rear one, and it only applies when accept names a type that can be captured.

<input type="file" accept="image/*" capture="environment">
<input type="file" accept="image/*" capture="user">

The value is only a preference, and the browser may fall back to its default camera. In practice:

  • Chrome on Android opens the camera app directly. The Chrome team's guide on capturing an image from the user states that the visitor loses the option of choosing an existing picture.
  • Safari on iOS does the same. In WebKit's file upload panel source, a capture type plus an available camera shows the camera immediately. Otherwise the usual menu appears, with the photo library, the camera and the file browser.
  • Desktop browsers ignore the attribute and open the normal file dialog. caniuse lists no desktop browser as supporting it.

So capture removes a choice: a visitor who already has the photo in the library must shoot it again. Without capture, accept="image/*" already offers both the camera and existing photos on Android and iOS. Keep it for the cases where a fresh photo is the point, and do not treat it as a control: from a desktop, any file can still be sent.

HEIC and HEIF photos from iPhones

iPhones store photos as HEIF (.heic files) when the camera format is set to High Efficiency. According to caniuse, only Safari 17 and later, on macOS and iOS, decodes HEIC. In Chrome, Edge and Firefox, an <img> preview of a HEIC file stays broken.

What the input delivers depends on the Safari version and on accept:

  • Before a WebKit change that landed in March 2024, iOS always transcoded HEIC to JPEG for file inputs.
  • Since that change, iOS follows macOS and transcodes only when the accepted image types are explicitly restricted and leave HEIC out. With accept="image/*" or no accept, the original HEIC can come through.

If your pipeline cannot read HEIC, use an explicit list, which asks Safari to convert:

<input type="file" accept="image/jpeg,image/png">

Behaviour on Android is not specified anywhere and varies by device, so the server should recognise HEIC and either convert it or reject it with a clear message.

multiple, file size and server validation

multiple lets the user select several files in one go. Without it, the standard caps the selection at one file.

<input type="file" name="photos" accept="image/*" multiple>

What it does not do:

  • No attribute sets a maximum number of files or a maximum size. You check files.length and file.size yourself.
  • A new selection replaces the previous one. It does not add to it.
  • Phones vary: caniuse marks multiple as partial in Chrome for Android, and on iOS the camera returns one photo at a time.

A size check in the browser saves the visitor a wasted upload:

const MAX_BYTES = 10 * 1024 * 1024;

input.addEventListener('change', () => {
  const tooBig = [...input.files].find((file) => file.size > MAX_BYTES);
  input.setCustomValidity(tooBig ? `${tooBig.name} is larger than 10 MB.` : '');
  input.reportValidity();
});

This is a courtesy, not a defence, because anyone can post to your endpoint without your page. The OWASP File Upload Cheat Sheet lists the server-side checks: an allowlist of extensions, no trust in the Content-Type header, a check of the file signature, a size limit, and a file name generated by the application.

Setting files from script with DataTransfer

You cannot write a path into a file input. The standard only lets you set value to the empty string, which clears the selection, and anything else throws an InvalidStateError. But input.files has a setter, and the DataTransfer constructor, available across browsers since April 2021, builds the FileList it expects.

function setFiles(input, files) {
  const list = input.multiple ? [...files] : [...files].slice(0, 1);
  const transfer = new DataTransfer();
  for (const file of list) transfer.items.add(file);

  input.files = transfer.files;
  input.dispatchEvent(new Event('input', { bubbles: true }));
  input.dispatchEvent(new Event('change', { bubbles: true }));
}

The browser fires input and change only for a selection made by the user, so dispatch them yourself.

This is not a security hole: a script can only place File objects it already holds, and can never read a path from the disk. It is legitimate when the user supplied the file through another door:

  • a drop zone or a paste handler that feeds the real input, so the form still submits natively
  • removing one file from a multiple selection
  • replacing a photo with a resized version that the user can see
  • receiving a file from another device

Drag and drop and paste as extra entry points

Both hand you a DataTransfer, so both can reuse setFiles:

const zone = document.querySelector('#dropzone');

zone.addEventListener('dragover', (event) => event.preventDefault());

zone.addEventListener('drop', (event) => {
  event.preventDefault();
  setFiles(input, event.dataTransfer.files);
});

document.addEventListener('paste', (event) => {
  const { files } = event.clipboardData;
  if (files.length === 0) return;
  event.preventDefault();
  setFiles(input, files);
});

Cancelling dragover is what allows drop to fire, as MDN's file drag and drop guide explains. accept does not apply here, so filter the files yourself. And keep the input reachable: drag and drop is not usable by everyone, and file pasting should be tested in each browser you target.

Accessible custom-styled file inputs

The safe pattern keeps the real input in the page, visually hidden, with a real <label> styled as the button:

<input class="visually-hidden" id="doc" name="doc" type="file" accept="image/*,.pdf">
<label class="file-button" for="doc">Choose a document</label>
<p id="doc-status" role="status"></p>

<style>
  .visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
  }

  .visually-hidden:focus-visible + .file-button {
    outline: 2px solid;
    outline-offset: 2px;
  }
</style>
  • Do not use display: none or visibility: hidden. MDN warns that assistive technology then treats the input as non-interactive, and recommends opacity instead. A display: none input also leaves the tab order, so keyboard users cannot open the picker.
  • Use a real label tied to the input with for. Clicking it opens the picker without any script.
  • Show the focus. Focus sits on the hidden input, so draw the ring on the label. A visible keyboard focus is WCAG success criterion 2.4.7.
  • Say what was chosen. Write the file name or the error into the role="status" element so that it is announced.

The gap no attribute closes: the photo is on the phone

Everything above assumes the file is on the device that shows the form. Often the visitor is on a computer, and the receipt or the ID is on the phone, or still has to be photographed. capture does nothing on a desktop, and no attribute reaches another device. The options:

  • Let the visitor cope with email, a cable or a cloud drive. It takes minutes, and some people give up.
  • Move the form. A link or a QR code reopens the page on the phone, where the visitor starts again unless you transfer the session.
  • Use the webcam with getUserMedia. Acceptable for a face, poor for a document.
  • Bring the file to the computer. BeamField adds a "Use phone" button beside an existing <input type="file">. The visitor scans a QR code, takes or picks a photo on the phone, and the file arrives in the same input on the computer as a normal File, set through DataTransfer with input and change events, so your upload and validation code continues unchanged. The file travels directly between the visitor's two browsers over a WebRTC data channel, end-to-end encrypted with AES-256-GCM using a key carried in the QR code and never sent to the server, and BeamField's servers do not store it. Integration is one script snippet: see the live demo on the home page, the integration lab or the documentation.

Compatibility summary

BehaviourChrome on AndroidSafari on iOSDesktop browsers
accept filters the pickerPartialPartialYes, but the user can usually show all files
accept="image/*" offers the cameraYesYesNo
captureOpens the camera, no existing photosOpens the camera when one is availableIgnored
multiplePartialYesYes
Displays HEIC imagesNoSafari 17 and laterSafari 17 and later only
Converts HEIC to JPEG on selectionVaries by deviceWhen accept lists explicit types without HEICSafari on macOS, same rule

FAQ

Does accept stop people from uploading the wrong file type?

No. It filters what the picker shows, and the user can often override it. Check the type and the size again on the server.

Why does capture do nothing on my laptop?

Desktop browsers do not implement HTML Media Capture. They ignore the attribute and open the ordinary file dialog.

Why do I receive .heic files, and how do I avoid them?

With accept="image/*" or no accept, recent versions of Safari can pass the iPhone's original HEIC file. List the formats you support, for example accept="image/jpeg,image/png", so that Safari converts the photo.

Can JavaScript put a file into a file input?

Yes, with File objects the page already holds: assign the files of a DataTransfer to input.files, then dispatch input and change. It cannot pick a file from the disk by path.

How can a visitor on a computer add a photo that is on their phone?

Not with an attribute. Either the visitor moves the photo by hand, or the form moves to the phone, or a handoff such as BeamField sends the photo from the phone into the input on the computer. More references are listed in all guides.

More guides