--- url: "https://web.dev/articles/media-capturing-images" titulek: "Capturing an image from the user | Articles | web.dev" h1: "Capturing an image from the user Stay organized with collections Save and categorize content based on your preferences." popis: "Most browsers can get access to the user's camera." jazyk: "en" kanonicka: "https://web.dev/articles/media-capturing-images" drobecky: ["Articles", "Capturing an image from the user"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:29:15" znaku: 12726 slov: 1899 kodovani: "iso-8859-2" sha256: "6bd3102d65122f630c9553e5de5a324fba467537ef0a2606fddb3bb55a69a629" --- Capturing an image from the user Stay organized with collections Save and categorize content based on your preferences. Most browsers can get access to the user's camera. Mat Scales Paul Kinlan Many browsers now have the ability to access video and audio input from the user. However, depending on the browser it might be a full dynamic and inline experience, or it could be delegated to another app on the user's device. On top of that, not every device even has a camera. So how can you create an experience that uses a user generated image that works well everywhere? Start simple and progressively If you want to progressively enhance your experience, you need to start with something that works everywhere. The easiest thing to do is simply ask the user for a pre-recorded file. Ask for a URL This is the best supported but least satisfying option. Get the user to give you a URL, and then use that. For just displaying the image this works everywhere. Create an `img` element, set the `src` and you're done. Though, if you want to manipulate the image in any way, things are a bit more complicated. [CORS](https://developer.mozilla.org/docs/Web/HTTP/Access_control_CORS) prevents you from accessing the actual pixels unless the server sets the appropriate headers and you [mark the image as crossorigin](https://developer.mozilla.org/docs/Web/HTML/CORS_enabled_image) ; the only practical way around that is to run a proxy server. File input You can also use a simple file input element, including an `accept` filter that indicates you only want image files. `` This method works on all platforms. On desktop it will prompt the user to upload an image file from the file system. In Chrome and Safari on iOS and Android this method will give the user a choice of which app to use to capture the image, including the option of taking a photo directly with the camera or choosing an existing image file. The data can then be attached to a `
` or manipulated with JavaScript by listening for an `onchange` event on the input element and then reading the `files` property of the event `target`. ` ` The `files` property is a `FileList` object, which I'll talk more about later. You can also optionally add the `capture` attribute to the element, which indicates to the browser that you prefer getting an image from the camera. ` ` Adding the `capture` attribute without a value let's the browser decide which camera to use, while the `"user"` and `"environment"` values tell the browser to prefer the front and rear cameras, respectively. The `capture` attribute works on Android and iOS, but is ignored on desktop. Be aware, however, that on Android this means that the user will no longer have the option of choosing an existing picture. The system camera app will be started directly, instead. Drag and drop If you are already adding the ability to upload a file, there are a couple of easy ways that you can make the user experience a little richer. The first is to add a drop target to your page that allows the user to drag in a file from the desktop or another application. `
You can drag an image file here
` Similar to the file input, you can get a `FileList` object from the `dataTransfer.files` property of the `drop` event; The `dragover` event handler lets you signal to the user what will happen when they drop the file by using [the `dropEffect` property](https://developer.mozilla.org/docs/Web/API/DataTransfer/dropEffect). Drag and drop has been around for a long time and is well-supported by the major browsers. Paste from clipboard The final way to get an existing image file is from the clipboard. The code for this is very simple, but the user experience is a little harder to get right. ` ` (`e.clipboardData.files` is yet another `FileList` object.) The tricky part with the clipboard API is that, for full cross-browser support, the target element needs to be both selectable and editable. Both `