--- url: "https://web.dev/articles/files/drag-and-drop-files" titulek: "How to drag and drop files | Files and directories patterns | web.dev" h1: "How to drag and drop files Stay organized with collections Save and categorize content based on your preferences." popis: "" jazyk: "en" kanonicka: "https://web.dev/articles/files/drag-and-drop-files" drobecky: ["Files and directories patterns", "How to drag and drop files"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 01:47:33" znaku: 6476 slov: 822 kodovani: "iso-8859-2" sha256: "bc366a979f1c85f1aac38bc042ebab4fc6cc9012fde26928bdd282c624e22579" --- How to drag and drop files Stay organized with collections Save and categorize content based on your preferences. Thomas Steiner The [HTML Drag and Drop interfaces](https://developer.mozilla.org/docs/Web/API/HTML_Drag_and_Drop_API) enable web applications to accept [dragged and dropped files](https://developer.mozilla.org/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop) on a web page. During a drag and drop operation, dragged file and directory items are associated with file entries and directory entries respectively. When it comes to dragging and dropping files into the browser, there are two ways of doing it: the modern and the classic way. The modern way Using the File System Access API's `DataTransferItem.getAsFileSystemHandle()` method The `DataTransferItem.getAsFileSystemHandle()` method returns a promise with a `FileSystemFileHandle` object if the dragged item is a file, and a promise with a `FileSystemDirectoryHandle` object if the dragged item is a directory. These handles let you read, and optionally write back to the file or directory. Note that the Drag and Drop interface's [`DataTransferItem.kind`](https://developer.mozilla.org/docs/Web/API/DataTransferItem/kind) will be `"file"` for both files *and* directories, whereas the File System Access API's [`FileSystemHandle.kind`](https://wicg.github.io/file-system-access/#dom-filesystemhandle-kind) will be `"file"` for files and `"directory"` for directories. Browser Support 86 86 x x [Source](https://developer.mozilla.org/docs/Web/API/DataTransferItem/getAsFileSystemHandle) The classic way Using the classic `DataTransferItem.getAsFile()` method The `DataTransferItem.getAsFile()` method returns the drag data item's `File` object. If the item is not a file, this method returns `null`. While you can read the file, there is no way to write back to it. This method has the disadvantage that it does not support directories. Browser Support 11 12 50 5.1 [Source](https://developer.mozilla.org/docs/Web/API/DataTransferItem/getAsFile) Progressive enhancement The snippet below uses the modern File System Access API's `DataTransferItem.getAsFileSystemHandle()` method when it is supported, then falls back to the non-standard `DataTransferItem.webkitGetAsEntry()` method, and finally falls back to the classic `DataTransferItem.getAsFile()` method. Be sure to check the type of each `handle`, since it could be either of: `FileSystemFileHandle` when the modern code path is chosen. `File` when the classic code path is chosen. All types have a `name` property, so logging it is fine and will always work. `// Run feature detection. const supportsFileSystemAccessAPI = 'getAsFileSystemHandle' in DataTransferItem.prototype; // This is the drag and drop zone. const elem = document.querySelector('main'); // Prevent navigation. elem.addEventListener('dragover', (e) => { e.preventDefault(); }); // Visually highlight the drop zone. elem.addEventListener('dragenter', (e) => { elem.style.outline = 'solid red 1px'; }); // Visually unhighlight the drop zone. elem.addEventListener('dragleave', (e) => { elem.style.outline = ''; }); // This is where the drop is handled. elem.addEventListener('drop', async (e) => { // Prevent navigation. e.preventDefault(); // Unhighlight the drop zone. elem.style.outline = ''; // Prepare an array of promises… const fileHandlesPromises = [...e.dataTransfer.items] // …by including only files (where file misleadingly means actual file _or_ // directory)… .filter((item) => item.kind === 'file') // …and, depending on previous feature detection… .map((item) => supportsFileSystemAccessAPI // …either get a modern `FileSystemHandle`… ? item.getAsFileSystemHandle() // …or a classic `File`. : item.getAsFile(), ); // Loop over the array of promises. for await (const handle of fileHandlesPromises) { // This is where we can actually exclusively act on the files. if (handle.kind === 'file' || handle.isFile) { console.log(`File: ${handle.name}`); } } });` Further reading [File System Access API](/file-system-access) [Drag and Drop API](/drag-and-drop) Demo HTML ` How to drag and drop files

How to drag and drop files

Drag and drop one or multiple files onto the page.


 
` CSS `:root { color-scheme: dark light; box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; } body { margin: 0; padding: 1rem; font-family: system-ui, sans-serif; line-height: 1.5; min-height: 100vh; display: flex; flex-direction: column; } img, video { height: auto; max-width: 100%; } main { flex-grow: 1; } footer { margin-top: 1rem; border-top: solid CanvasText 1px; font-size: 0.8rem; }` JS `const supportsFileSystemAccessAPI = "getAsFileSystemHandle" in DataTransferItem.prototype; const supportsWebkitGetAsEntry = "webkitGetAsEntry" in DataTransferItem.prototype; const elem = document.querySelector("main"); const debug = document.querySelector("pre"); elem.addEventListener("dragover", (e) => { // Prevent navigation. e.preventDefault(); }); elem.addEventListener("dragenter", (e) => { elem.style.outline = "solid red 1px"; }); elem.addEventListener("dragleave", (e) => { elem.style.outline = ""; }); elem.addEventListener("drop", async (e) => { e.preventDefault(); elem.style.outline = ""; const fileHandlesPromises = [...e.dataTransfer.items] .filter((item) => item.kind === "file") .map((item) => supportsFileSystemAccessAPI ? item.getAsFileSystemHandle() : supportsWebkitGetAsEntry ? item.webkitGetAsEntry() : item.getAsFile() ); for await (const handle of fileHandlesPromises) { if (handle.kind === "directory" || handle.isDirectory) { console.log(`Directory: ${handle.name}`); debug.textContent += `Directory: ${handle.name}\n`; } else { console.log(`File: ${handle.name}`); debug.textContent += `File: ${handle.name}\n`; } } });` 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.