CKTIS / HTML
Atrybut capture w polu plikowym: user i environment
Atrybut `capture` w polu plikowym sugeruje sposób utworzenia nowego pliku multimedialnego. Dla zdjęć i filmów `user` preferuje aparat skierowany na użytkownika, a `environment` — aparat skierowany na otoczenie.
Wersja: W3C HTML Media Capture Recommendation (2018); implementation support varies
Składnia
<input type="file" accept="image/*" capture="user">
<input type="file" accept="image/*" capture="environment">capture to atrybut HTML Media Capture używany w elemencie input z type="file". Podaj też accept, aby wskazać oczekiwany rodzaj pliku: image/* dla obrazów, video/* dla filmów lub audio/* dla dźwięku. Pole nadal służy do wyboru pliku; atrybut nie udostępnia stronie strumienia na żywo ani nie wysyła pliku.
Dla zdjęć i filmów capture="user" wskazuje preferencję aparatu skierowanego na użytkownika, zwykle przedniego. capture="environment" wskazuje aparat skierowany na otoczenie, zwykle tylny. Podawaj wartość wprost: samo capture nie wybiera żadnego z tych dwóch kierunków.
To wskazówka, nie wymóg. Jeśli preferowany kierunek jest niedostępny, przeglądarka może wybrać ustawienie domyślne. W zależności od urządzenia i obsługi atrybutu może też wyświetlić menu lub zwykły wybór pliku. Jeśli ważny jest wybór istniejącego pliku, udostępnij osobne pole bez capture. accept nie weryfikuje zawartości: jeśli przyjmujesz pliki na serwerze, sprawdzaj je po jego stronie.
Przykłady
<!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>Wynik
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.
Pierwsze pole preferuje aparat przedni, drugie — tylny, a trzecie nie prosi o rejestrowanie obrazu. Porównaj, co otwiera każde pole na telefonie i na komputerze. Przykład niczego nie wysyła.
Powiązane lekcje
- HTML · Lekcja 7 · Średniozaawansowany · Wersja: 2