Baza znanjaCentar za ispite
HRHrvatski
Odaberi jezik13

CKTIS / HTML

Atribut capture u polju za odabir datoteke: user i environment

Atribut `capture` u polju za odabir datoteke daje pregledniku smjernicu za stvaranje nove medijske datoteke. Za fotografije i videozapise `user` daje prednost prednjoj kameri, a `environment` kameri okrenutoj prema okolini.

Verzija: 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 specifikacije HTML Media Capture koji se koristi na elementu input s type="file". Pomoću accept naznači očekivanu vrstu datoteke: image/* za slike, video/* za videozapise ili audio/* za zvuk. To je i dalje polje za odabir datoteke: atribut stranici ne daje prijenos uživo niti šalje datoteku.

Za fotografije i videozapise capture="user" daje prednost kameri okrenutoj prema korisniku, obično prednjoj. capture="environment" daje prednost kameri okrenutoj prema okolini, obično stražnjoj. Izričito navedi vrijednost: sam capture ne odabire nijedan od ta dva smjera.

To je smjernica, a ne obveza. Ako željeni smjer nije dostupan, preglednik može primijeniti zadani način rada. Ovisno o uređaju i podršci preglednika može prikazati i izbornik ili uobičajeni prozor za odabir datoteke. Ako je važno omogućiti odabir postojeće datoteke, ponudi zasebno polje bez capture. accept ne provjerava sadržaj datoteka: ako ih primaš na poslužitelju, provjeri ih ondje.

Primjeri

<!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 prednjoj kameri, drugo stražnjoj, a treće ne traži snimanje. Usporedi što se otvara na mobilnom uređaju i na računalu. Primjer ne šalje datoteke.

Povezane lekcije

  • HTML · Lekcija 7 · Srednje · Verzija: 2

Izvori