Skip to content

Commit 9d5666d

Browse files
Josh-Cenabsmthgithub-actions[bot]
authored
Internalize DataTransfer.files example (#40316)
* Internalize DataTransfer.files example * Remove more * Apply suggestions from code review Co-authored-by: Brian Smith <brian@smith.berlin> * Update files/en-us/web/api/datatransfer/files/index.md Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update files/en-us/web/api/datatransfer/files/index.md Co-authored-by: Brian Smith <brian@smith.berlin> --------- Co-authored-by: Brian Smith <brian@smith.berlin> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 2547f62 commit 9d5666d

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

files/en-us/web/api/datatransfer/files/index.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,56 @@ This feature can be used to drag files from a user's desktop to the browser.
1717
1818
## Value
1919

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

2323
## Examples
2424

25-
There are two live examples of this interface:
25+
### Reading the files list
2626

27-
- Firefox only: <https://jsfiddle.net/9C2EF/>
28-
- All browsers: [https://jsbin.com/hiqasek/](https://jsbin.com/hiqasek/edit?html,js,output)
27+
This example creates a basic area that you can drop files into and displays some metadata.
28+
29+
```html
30+
<pre id="output">Drop files here from your file system.</pre>
31+
```
32+
33+
```css
34+
#output {
35+
min-height: 200px;
36+
border: 1px solid black;
37+
padding: 1em;
38+
}
39+
```
40+
41+
```js
42+
const output = document.getElementById("output");
43+
44+
function log(text) {
45+
output.innerText += text;
46+
}
47+
48+
output.addEventListener("dragenter", (e) => {
49+
e.stopPropagation();
50+
e.preventDefault();
51+
output.textContent = "";
52+
});
53+
output.addEventListener("dragover", (e) => {
54+
e.stopPropagation();
55+
e.preventDefault();
56+
});
57+
output.addEventListener("drop", (e) => {
58+
e.stopPropagation();
59+
e.preventDefault();
60+
const files = event.dataTransfer.files;
61+
log(`File Count: ${files.length}\n`);
62+
63+
for (const file of files) {
64+
log(` File: ${file}, ${file.name}, ${file.size} bytes\n`);
65+
}
66+
});
67+
```
68+
69+
{{EmbedLiveSample("reading_the_files_list", "", "300")}}
2970

3071
## Specifications
3172

files/en-us/web/api/html_drag_and_drop_api/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ A key difference between the {{domxref("DataTransfer")}} and {{domxref("DataTran
228228

229229
- [Copying and moving elements with the `DataTransfer` interface](https://mdn.github.io/dom-examples/drag-and-drop/copy-move-DataTransfer.html)
230230
- [Copying and moving elements with the `DataTransferListItem` interface](https://mdn.github.io/dom-examples/drag-and-drop/copy-move-DataTransferItemList.html)
231-
- Dragging and dropping files (Firefox only): <https://jsfiddle.net/9C2EF/>
232-
- Dragging and dropping files (All browsers): [https://jsbin.com/hiqasek/](https://jsbin.com/hiqasek/edit?html,js,output)
231+
232+
Reference pages for each interface also have individual examples.
233233

234234
## Specifications
235235

0 commit comments

Comments
 (0)