Skip to content

Internalize DataTransfer.files example #40316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions files/en-us/web/api/datatransfer/files/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,56 @@ This feature can be used to drag files from a user's desktop to the browser.

## Value

A {{domxref("FileList","list")}} of the files in a drag operation, one list item for
A {{domxref("FileList")}} of the files in a drag operation, one list item for
each file in the operation. If the drag operation had no files, the list is empty.

## Examples

There are two live examples of this interface:
### Reading the files list

- Firefox only: <https://jsfiddle.net/9C2EF/>
- All browsers: [https://jsbin.com/hiqasek/](https://jsbin.com/hiqasek/edit?html,js,output)
This example creates a basic area that you can drop files into and displays some metadata.

```html
<pre id="output">Drop files here from your file system.</pre>
```

```css
#output {
min-height: 200px;
border: 1px solid black;
padding: 1em;
}
```

```js
const output = document.getElementById("output");

function log(text) {
output.innerText = text;
}

output.addEventListener("dragenter", (e) => {
e.stopPropagation();
e.preventDefault();
output.textContent = "";
});
output.addEventListener("dragover", (e) => {
e.stopPropagation();
e.preventDefault();
});
output.addEventListener("drop", (e) => {
e.stopPropagation();
e.preventDefault();
const files = event.dataTransfer.files;
log(`File Count: ${files.length}\n`);

for (const file of files) {
log(` File: ${file}, ${file.name}, ${file.size} bytes\n`);
}
});
```

{{EmbedLiveSample("reading_the_files_list", "", "300")}}

## Specifications

Expand Down
4 changes: 2 additions & 2 deletions files/en-us/web/api/html_drag_and_drop_api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ A key difference between the {{domxref("DataTransfer")}} and {{domxref("DataTran

- [Copying and moving elements with the `DataTransfer` interface](https://mdn.github.io/dom-examples/drag-and-drop/copy-move-DataTransfer.html)
- [Copying and moving elements with the `DataTransferListItem` interface](https://mdn.github.io/dom-examples/drag-and-drop/copy-move-DataTransferItemList.html)
- Dragging and dropping files (Firefox only): <https://jsfiddle.net/9C2EF/>
- Dragging and dropping files (All browsers): [https://jsbin.com/hiqasek/](https://jsbin.com/hiqasek/edit?html,js,output)

Reference pages for each interface also have individual examples.

## Specifications

Expand Down