--- url: "https://web.dev/articles/clipboard/paste-files" titulek: "How to paste files | Clipboard | web.dev" h1: "How to paste files Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/clipboard/paste-files" drobecky: ["Clipboard", "How to paste files"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:05:08" znaku: 3853 slov: 534 kodovani: "iso-8859-2" sha256: "ab3c1d63439ad1426eec863e38ce1cb701ed50049c2576a2735938a908d8a55e" --- How to paste files Stay organized with collections Save and categorize content based on your preferences. Harry Theodoulou Pasting files into the browser consists of using the `paste` event of the `HTMLElement`. Using the `HTMLElement`'s `paste` event As a first step, you add an event listener for the `paste` event at the desired element, commonly this is at the `document` level, so no specific element needs to be focused. Next, you use the [Clipboard API](https://developer.mozilla.org/docs/Web/API/Clipboard_API), which gives you access to the `clipboardData` field of the `HTMLElement`'s `paste` event, whose `files` list you can then iterate over. Based on the MIME type of each of the files pasted, you can decide whether to render it to the screen, as in the case of an image or video, or to paste the text contents of the file into, for example, a `textarea` element, in the case of a text file. Browser Support 66 79 63 13.1 [Source](https://developer.mozilla.org/docs/Web/API/Clipboard) `document.addEventListener('paste', async (e) => { // Prevent the default behavior, so you can code your own logic. e.preventDefault(); if (!e.clipboardData.files.length) { return; } // Iterate over all pasted files. Array.from(e.clipboardData.files).forEach(async (file) => { // Add more checks here for MIME types you're interested in, // such as `application/pdf`, `video/mp4`, etc. if (file.type.startsWith('image/')) { // For images, create an image and append it to the `body`. const img = document.createElement('img'); const blob = URL.createObjectURL(file); img.src = blob; document.body.append(img); } else if (file.type.startsWith('text/')) { // For text files, read the contents and output it into a `textarea`. const textarea = document.createElement('textarea'); textarea.value = await file.text(); document.body.append(textarea); } }); });` Further reading [Clipboard API](https://developer.mozilla.org/docs/Web/API/Clipboard_API) [Unblocking clipboard access](/async-clipboard) Demo HTML `
Hit â + v (for macOS) or ctrl + v (for other operating systems) to paste image or text file(s) anywhere in this page.
` CSS `html { box-sizing: border-box; font-family: system-ui, sans-serif; color-scheme: dark light; } *, *:before, *:after { box-sizing: inherit; } body { margin: 1rem; } img { height: auto; max-width: 100%; display: block; }` JS `document.addEventListener('paste', async (e) => { // Prevent the default behavior, so you can code your own logic. e.preventDefault(); if (!e.clipboardData.files.length) { return; } // Iterate over all pasted files. Array.from(e.clipboardData.files).forEach(async (file) => { // Add more checks here for MIME types you're interested in, // such as `application/pdf`, `video/mp4`, etc. if (file.type.startsWith('image/')) { // For images, create an image and append it to the `body`. const img = document.createElement('img'); const blob = URL.createObjectURL(file); img.src = blob; document.body.append(img); } else if (file.type.startsWith('text/')) { // For text files, read the contents and output it into a `textarea`. const textarea = document.createElement('textarea'); textarea.value = await file.text(); document.body.append(textarea); } }); });` Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2026-09-04 UTC.