WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / HTML

Das Attribut capture bei Dateifeldern: user und environment

Das Attribut `capture` gibt bei einem Dateifeld einen Hinweis zur Aufnahme einer neuen Mediendatei. Für Fotos und Videos bevorzugt `user` die Frontkamera, `environment` die nach außen gerichtete Kamera.

Version: W3C HTML Media Capture Recommendation (2018); implementation support varies

Syntax

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

capture ist ein Attribut aus HTML Media Capture für ein input mit type="file". Mit accept gibst du die erwartete Dateiart an: image/* für Bilder, video/* für Videos oder audio/* für Audio. Das Feld bleibt ein Dateifeld: Das Attribut stellt der Seite weder einen Livestream bereit noch lädt es die Datei hoch.

Bei Fotos und Videos bevorzugt capture="user" eine zur Person gerichtete Kamera, meist die Frontkamera. capture="environment" bevorzugt eine auf die Umgebung gerichtete Kamera, meist die Rückkamera. Gib den Wert ausdrücklich an: capture ohne Wert wählt keine der beiden Richtungen aus.

Das ist ein Hinweis, keine Vorgabe. Ist die gewünschte Ausrichtung nicht verfügbar, kann der Browser seine Standardeinstellung verwenden. Je nach Gerät und Browserunterstützung kann er auch ein Menü oder einen gewöhnlichen Dateiauswahldialog öffnen. Wenn vorhandene Dateien auswählbar sein sollen, biete dafür ein separates Feld ohne capture an. accept prüft den Dateiinhalt nicht: Prüfe hochgeladene Dateien auf dem Server.

Beispiele

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Photo capture preferences</title>
</head>
<body>
  <p>
    <label for="front-photo">Take a selfie (front camera preferred)</label><br>
    <input id="front-photo" type="file" accept="image/*" capture="user">
  </p>
  <p>
    <label for="rear-photo">Take a photo (rear camera preferred)</label><br>
    <input id="rear-photo" type="file" accept="image/*" capture="environment">
  </p>
  <p>
    <label for="image-file">Choose an image file</label><br>
    <input id="image-file" type="file" accept="image/*">
  </p>
  <script>
    const controls = [...document.querySelectorAll('input[type="file"]')];
    const descriptions = new Map([
      ['user', 'requests a front-facing camera'],
      ['environment', 'requests an outward-facing camera'],
      [null, 'makes no capture request']
    ]);
    const expectedPreferences = [...descriptions.keys()];
    const isVisible = element =>
      element.getClientRects().length > 0 &&
      getComputedStyle(element).visibility === 'visible';

    // Check the controls and their markup, not which picker or camera opens.
    if (controls.length !== expectedPreferences.length ||
        !controls.every((control, index) =>
          isVisible(control) &&
          [...control.labels].some(label => isVisible(label) && label.innerText.trim()) &&
          control.accept === 'image/*' &&
          control.getAttribute('capture') === expectedPreferences[index] &&
          !control.form)) {
      throw new Error('Expected three visible, labeled image file controls with the specified capture preferences and no upload form.');
    }

    const [first, second, third] = controls.map(control =>
      descriptions.get(control.getAttribute('capture'))
    );
    console.log(
      'The page displays three labeled image file controls. ' +
      `The first ${first}, the second ${second}, and the third ${third}. ` +
      'The browser and device determine what opens when each control is activated. ' +
      'The page does not upload anything.'
    );
  </script>
</body>
</html>

Ergebnis

The page displays three labeled image file controls. The first requests a front-facing camera, the second requests an outward-facing camera, and the third makes no capture request. The browser and device determine what opens when each control is activated. The page does not upload anything.

Das erste Feld bevorzugt die Frontkamera, das zweite die Rückkamera, und das dritte fordert keine Aufnahme an. Vergleiche auf einem Mobilgerät und einem Desktoprechner, was sich jeweils öffnet. Das Beispiel lädt nichts hoch.

Zugehörige Lektionen

  • HTML · Lektion 7 · Mittlere Stufe · Version: 2

Quellen