Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / HTML

Atribut capture v polju za izbiro datoteke: user in environment

Atribut `capture` v polju za izbiro datoteke nakaže, kako naj nastane nova večpredstavnostna datoteka. Pri fotografijah in videih `user` daje prednost sprednji kameri, `environment` pa kameri, obrnjeni proti okolici.

Različica: W3C HTML Media Capture Recommendation (2018); implementation support varies

Sintaksa

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

capture je atribut iz HTML Media Capture za element input z type="file". Z atributom accept določi pričakovano vrsto datoteke: image/* za slike, video/* za video ali audio/* za zvok. To je še vedno polje za izbiro datoteke: atribut strani ne zagotovi prenosa v živo in datoteke ne pošlje.

Pri fotografijah in videih capture="user" daje prednost kameri, obrnjeni proti uporabniku, običajno sprednji. capture="environment" daje prednost kameri, obrnjeni proti okolici, običajno zadnji. Vrednost zapiši izrecno: capture brez vrednosti ne izbere nobene od teh dveh smeri.

To je namig, ne zahteva. Če želena smer ni na voljo, lahko brskalnik uporabi privzeti način. Glede na napravo in podporo v brskalniku se lahko odpre tudi meni ali običajno okno za izbiro datoteke. Če je izbira obstoječe datoteke pomembna, ponudi ločeno polje brez capture. accept ne preverja vsebine: če datoteke prejemaš na strežniku, jih preveri tam.

Primeri

<!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>

Rezultat

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.

Prvo polje daje prednost sprednji kameri, drugo zadnji, tretje pa ne zahteva zajema. Primerjaj, kaj se odpre na mobilni napravi in kaj na računalniku. Primer ne pošlje nobene datoteke.

Povezane lekcije

  • HTML · Lekcija 7 · Srednja · Različica: 2

Viri