Does HTML5 allow drag-drop upload of folders or a folder tree?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I haven't seen any examples that do this. Is this not allowed in the API spec?
I am searching for an easy drag-drop solution for uploading an entire folder tree of photos.
html5 file-upload drag-and-drop
add a comment |
I haven't seen any examples that do this. Is this not allowed in the API spec?
I am searching for an easy drag-drop solution for uploading an entire folder tree of photos.
html5 file-upload drag-and-drop
Same forinput type=file
: stackoverflow.com/questions/9518335/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:52
npm package npmjs.com/package/datatransfer-files-promise
– grabantot
Oct 20 '18 at 19:56
add a comment |
I haven't seen any examples that do this. Is this not allowed in the API spec?
I am searching for an easy drag-drop solution for uploading an entire folder tree of photos.
html5 file-upload drag-and-drop
I haven't seen any examples that do this. Is this not allowed in the API spec?
I am searching for an easy drag-drop solution for uploading an entire folder tree of photos.
html5 file-upload drag-and-drop
html5 file-upload drag-and-drop
edited Nov 4 '18 at 5:58
xlm
3,18493440
3,18493440
asked Aug 28 '10 at 8:33
michaelmichael
1,32632448
1,32632448
Same forinput type=file
: stackoverflow.com/questions/9518335/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:52
npm package npmjs.com/package/datatransfer-files-promise
– grabantot
Oct 20 '18 at 19:56
add a comment |
Same forinput type=file
: stackoverflow.com/questions/9518335/…
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:52
npm package npmjs.com/package/datatransfer-files-promise
– grabantot
Oct 20 '18 at 19:56
Same for
input type=file
: stackoverflow.com/questions/9518335/…– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:52
Same for
input type=file
: stackoverflow.com/questions/9518335/…– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:52
npm package npmjs.com/package/datatransfer-files-promise
– grabantot
Oct 20 '18 at 19:56
npm package npmjs.com/package/datatransfer-files-promise
– grabantot
Oct 20 '18 at 19:56
add a comment |
9 Answers
9
active
oldest
votes
It's now possible, thanks to Chrome >= 21.
function traverseFileTree(item, path) "";
if (item.isFile)
// Get file
item.file(function(file)
console.log("File:", path + file.name);
);
else if (item.isDirectory)
// Get folder contents
var dirReader = item.createReader();
dirReader.readEntries(function(entries)
for (var i=0; i<entries.length; i++)
traverseFileTree(entries[i], path + item.name + "/");
);
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
for (var i=0; i<items.length; i++)
// webkitGetAsEntry is where the magic happens
var item = items[i].webkitGetAsEntry();
if (item)
traverseFileTree(item);
, false);
More info: https://protonet.info/blog/html5-experiment-drag-drop-of-folders/
9
Even 2 years later, IE and Firefox don't seem to be willing to implement this.
– Nicolas Raoul
Jul 3 '14 at 9:09
8
Now, for Firefox as well: stackoverflow.com/a/33431704/195216 It shows folder uploading via drag'n'drop and via dialog in chrome and firefox!
– dforce
Nov 5 '15 at 7:42
2
Edge supports this too.
– ZachB
Dec 9 '16 at 22:52
3
Important warning: The code in this answer is limited to 100 files in a given directory. See here : bugs.chromium.org/p/chromium/issues/detail?id=514087
– johnozbay
Mar 2 '18 at 6:26
1
@johnozbay it's unfortunate that more people picked up your important warning, and it's not necessarily a Chromium issue since the spec saysreadEntries
won't return all the entires in a directory. Based on the bug link your provided, I've written up a complete answer: stackoverflow.com/a/53058574/885922
– xlm
Oct 30 '18 at 23:42
|
show 7 more comments
In this message to the HTML 5 mailing list Ian Hickson says:
HTML5 now has to upload many files at
once. Browsers could allow users to
pick multiple files at once, including
across multiple directories; that's a
bit out of scope of the spec.
(Also see the original feature proposal.)
So it's safe to assume he considers uploading folders using drag-and-drop also out of scope. Apparently it's up to the browser to serve individual files.
Uploading folders would also have some other difficulties, as described by Lars Gunther:
This […] proposal must have two
checks (if it is doable at all):
Max size, to stop someone from uploading a full directory of several
hundred uncompressed raw images...
Filtering even if the accept attribute is omitted. Mac OS metadata
and Windows thumbnails, etc should be
omitted. All hidden files and
directories should default to be
excluded.
Hmmm, I agree on point 2... but only as long as there is a way for the web developer to determine if they want to enable the upload of hidden files - as there is always the potential that a hidden file could be operative to the use of the uploaded folder. Especially if the folder is a full on document split into multiple parts like a final cut file might be.
– Charles John Thompson III
Aug 6 '14 at 14:57
Disagree with out of scope: this is a cause of incompatibilities for something many people want to do, so it should be specified.
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:50
add a comment |
Now you can upload directories with both drag and drop and input.
<input type='file' webkitdirectory >
and for drag and drop(For webkit browsers).
Handling drag and drop folders.
<div id="dropzone"></div>
<script>
var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e)
var length = e.dataTransfer.items.length;
for (var i = 0; i < length; i++)
var entry = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry.isFile)
... // do whatever you want
else if (entry.isDirectory)
... // do whatever you want
;
</script>
Resources:
http://updates.html5rocks.com/2012/07/Drag-and-drop-a-folder-onto-Chrome-now-available
1
Is it possible to do the same for downloading without using compressed folders ?
– user2284570
Aug 28 '15 at 9:38
add a comment |
Firefox now supports folder upload, as of November 15, 2016, in v50.0: https://developer.mozilla.org/en-US/Firefox/Releases/50#Files_and_directories
You can drag and drop folders into Firefox or you can browse and select a local folder to upload. It also supports folders nested in subfolders.
That means you can now use either Chrome, Firefox, Edge or Opera to upload folders. You can't use Safari or Internet Explorer at present.
add a comment |
This function will give you a promise for array of all dropped files, like <input type="file"/>.files
:
function getFilesWebkitDataTransferItems(dataTransferItems)
function traverseFileTreePromise(item, path='')
return new Promise( resolve =>
if (item.isFile)
item.file(file =>
file.filepath = path + file.name //save full path
files.push(file)
resolve(file)
)
else if (item.isDirectory)
let dirReader = item.createReader()
dirReader.readEntries(entries =>
let entriesPromises =
for (let entr of entries)
entriesPromises.push(traverseFileTreePromise(entr, path + item.name + "/"))
resolve(Promise.all(entriesPromises))
)
)
let files =
return new Promise((resolve, reject) =>
let entriesPromises =
for (let it of dataTransferItems)
entriesPromises.push(traverseFileTreePromise(it.webkitGetAsEntry()))
Promise.all(entriesPromises)
.then(entries =>
//console.log(entries)
resolve(files)
)
)
Usage:
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
getFilesFromWebkitDataTransferItems(items)
.then(files =>
...
)
, false);
npm package
https://www.npmjs.com/package/datatransfer-files-promise
usage example:
https://github.com/grabantot/datatransfer-files-promise/blob/master/index.html
4
This should be the new accepted answer. It is better than other answers because it returns a promise when complete. But there were a few mistakes:function getFilesWebkitDataTransferItems(dataTransfer)
should befunction getFilesWebkitDataTransferItems(items)
, andfor (entr of entries)
should befor (let entr of entries)
.
– RoccoB
Sep 3 '17 at 3:47
1
Won't actually get all the files in a directory (for Chrome it will only return 100 entries in a directory). Spec stipulates the need to callreadEntries
repeatedly until it returns an empty array.
– xlm
Oct 30 '18 at 23:28
@xlm Updated npm package. Now it handles >100 entries.
– grabantot
Nov 15 '18 at 16:01
add a comment |
Unfortunately none of the existing answers are completely correct because readEntries
will not necessarily return ALL the (file or directory) entries for a given directory. This is part of the API specification (see Documentation section below).
To actually get all the files, we'll need to call readEntries
repeatedly (for each directory we encounter) until it returns an empty array. If we don't, we will miss some files/sub-directories in a directory e.g. in Chrome, readEntries
will only return at most 100 entries at a time.
Using Promises (await
/ async
) to more clearly demonstrate the correct usage of readEntries
(since it's asynchronous), and BFS to traverse the directory structure:
// Drop handler function to get all files
async function getAllFileEntries(dataTransferItemList)
let fileEntries = ;
// Use BFS to traverse entire directory/file structure
let queue = ;
// Unfortunately dataTransferItemList is not iterable i.e. no forEach
for (let i = 0; i < dataTransferItemList.length; i++)
queue.push(dataTransferItemList[i].webkitGetAsEntry());
while (queue.length > 0)
let entry = queue.shift();
if (entry.isFile)
fileEntries.push(entry);
else if (entry.isDirectory)
queue.push(...await readAllDirectoryEntries(entry.createReader()));
return fileEntries;
// Get all the entries (files or sub-directories) in a directory
// by calling readEntries until it returns empty array
async function readAllDirectoryEntries(directoryReader)
let entries = ;
let readEntries = await readEntriesPromise(directoryReader);
while (readEntries.length > 0)
entries.push(...readEntries);
readEntries = await readEntriesPromise(directoryReader);
return entries;
// Wrap readEntries in a promise to make working with readEntries easier
// readEntries will return only some of the entries in a directory
// e.g. Chrome returns at most 100 entries at a time
async function readEntriesPromise(directoryReader)
try
return await new Promise((resolve, reject) =>
directoryReader.readEntries(resolve, reject);
);
catch (err)
console.log(err);
Complete working example on Codepen: https://codepen.io/anon/pen/gBJrOP
FWIW I only picked this up because I wasn't getting back all the files I expected in a directory containing 40,000 files (many directories containing well over 100 files/sub-directories) when using the accepted answer.
Documentation:
This behaviour is documented in FileSystemDirectoryReader. Excerpt with emphasis added:
readEntries()
Returns a an array containing some number of the
directory's entries. Each item in the array is an object based on
FileSystemEntry—typically either FileSystemFileEntry or
FileSystemDirectoryEntry.
But to be fair, the MDN documentation could make this clearer in other sections. The readEntries() documentation simply notes:
readEntries() method retrieves the directory entries within the directory being read and delivers them in an array to the provided callback function
And the only mention/hint that multiple calls are needed is in the description of successCallback parameter:
If there are no files left, or you've already called readEntries() on
this FileSystemDirectoryReader, the array is empty.
Arguably the API could be more intuitive as well, but as the documentation notes: it is a non-standard/experimental feature, not on a standards track, and can't be expected to work for all browsers.
Related:
johnozbay comments that on Chrome,readEntries
will return at most 100 entries for a directory (verified as Chrome 64).
Xan explains the correct usage ofreadEntries
quite well in this answer (albeit without code).
Pablo Barría Urenda's answer correctly callsreadEntries
in a asynchronous manner without BFS. He also notes that Firefox returns all the entries in a directory (unlike Chrome) but we can't rely on this given the specification.
3
Thanks a lot for the shout-out, and getting this content out there. SOF needs more fantastic members like yourself! ✌🏻
– johnozbay
Nov 1 '18 at 10:48
3
I appreciate that @johnozbay I'm just concerned that it seems that many users are overlooking this small but significant fact re: specification/API and this edge case (100+ files in a directory) isn't that unlikely. I only realised it when I wasn't getting back all the files I expected. Your comment should have been answer.
– xlm
Nov 2 '18 at 2:43
add a comment |
The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively.
Actually, in Chrome/Chromium, you can upload a folder, but when you do it, it just uploads a meaningless 4KB file, which represents the directory. Some servers-side applications like Alfresco can detect this, and warn the user that folders can not be uploaded:
A pointless file, or a pointer to a file?
– MoB
Aug 6 '12 at 9:02
@MoB: maybe it is some kind of pointer indeed. But since the actual file is on the client machine, the server machine will not be able to do anything with this pointer, of course.
– Nicolas Raoul
Aug 6 '12 at 10:37
add a comment |
Here's a complete example of how to use the file and directory entries API:
var dropzone = document.getElementById("dropzone");
var listing = document.getElementById("listing");
function scanAndLogFiles(item, container)
var elem = document.createElement("li");
elem.innerHTML = item.name;
container.appendChild(elem);
if (item.isDirectory)
var directoryReader = item.createReader();
var directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries)
entries.forEach(function(entry)
scanAndLogFiles(entry, directoryContainer);
);
);
dropzone.addEventListener(
"dragover",
function(event)
event.preventDefault();
,
false
);
dropzone.addEventListener(
"drop",
function(event)
var items = event.dataTransfer.items;
event.preventDefault();
listing.innerHTML = "";
for (var i = 0; i < items.length; i++)
var item = items[i].webkitGetAsEntry();
if (item)
scanAndLogFiles(item, listing);
,
false
);
body
font: 14px "Arial", sans-serif;
#dropzone
text-align: center;
width: 300px;
height: 100px;
margin: 10px;
padding: 10px;
border: 4px dashed red;
border-radius: 10px;
#boxtitle
display: table-cell;
vertical-align: middle;
text-align: center;
color: black;
font: bold 2em "Arial", sans-serif;
width: 300px;
height: 100px;
<p>Drag files and/or directories to the box below!</p>
<div id="dropzone">
<div id="boxtitle">
Drop Files Here
</div>
</div>
<h2>Directory tree:</h2>
<ul id="listing"></ul>
webkitGetAsEntry
is supported by Chrome 13+, Firefox 50+ and Edge.
Source: https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry
add a comment |
Does HTML5 allow drag-drop upload of folders or a folder tree?
Only Chrome supports this feature. It has failed to have any traction and is likely to be removed.
Ref : https://developer.mozilla.org/en/docs/Web/API/DirectoryReader#readEntries
Wow. Telling from the W3C Note at that link, this is indeed not continued. What is the basis of the assumption that it has failed to get any traction?
– bebbi
Feb 29 '16 at 12:58
@bebbi no other browser vendors implemented it
– basarat
Feb 29 '16 at 23:23
1
@PabloBarríaUrenda comment is not true; his issue is likely referring to his question: stackoverflow.com/questions/51850469/… which he solved/realisedreadEntries
can't be called if another call ofreadEntries
is still being run. The DirectoryReader API design isn't the best
– xlm
Oct 30 '18 at 23:37
@xlm yes, indeed you are correct. I had posted this while I myself was puzzled by the issue, but I eventually resolved it (and forgot about this comment). I have now deleted the confusing comment.
– Pablo Barría Urenda
Oct 31 '18 at 7:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3590058%2fdoes-html5-allow-drag-drop-upload-of-folders-or-a-folder-tree%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's now possible, thanks to Chrome >= 21.
function traverseFileTree(item, path) "";
if (item.isFile)
// Get file
item.file(function(file)
console.log("File:", path + file.name);
);
else if (item.isDirectory)
// Get folder contents
var dirReader = item.createReader();
dirReader.readEntries(function(entries)
for (var i=0; i<entries.length; i++)
traverseFileTree(entries[i], path + item.name + "/");
);
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
for (var i=0; i<items.length; i++)
// webkitGetAsEntry is where the magic happens
var item = items[i].webkitGetAsEntry();
if (item)
traverseFileTree(item);
, false);
More info: https://protonet.info/blog/html5-experiment-drag-drop-of-folders/
9
Even 2 years later, IE and Firefox don't seem to be willing to implement this.
– Nicolas Raoul
Jul 3 '14 at 9:09
8
Now, for Firefox as well: stackoverflow.com/a/33431704/195216 It shows folder uploading via drag'n'drop and via dialog in chrome and firefox!
– dforce
Nov 5 '15 at 7:42
2
Edge supports this too.
– ZachB
Dec 9 '16 at 22:52
3
Important warning: The code in this answer is limited to 100 files in a given directory. See here : bugs.chromium.org/p/chromium/issues/detail?id=514087
– johnozbay
Mar 2 '18 at 6:26
1
@johnozbay it's unfortunate that more people picked up your important warning, and it's not necessarily a Chromium issue since the spec saysreadEntries
won't return all the entires in a directory. Based on the bug link your provided, I've written up a complete answer: stackoverflow.com/a/53058574/885922
– xlm
Oct 30 '18 at 23:42
|
show 7 more comments
It's now possible, thanks to Chrome >= 21.
function traverseFileTree(item, path) "";
if (item.isFile)
// Get file
item.file(function(file)
console.log("File:", path + file.name);
);
else if (item.isDirectory)
// Get folder contents
var dirReader = item.createReader();
dirReader.readEntries(function(entries)
for (var i=0; i<entries.length; i++)
traverseFileTree(entries[i], path + item.name + "/");
);
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
for (var i=0; i<items.length; i++)
// webkitGetAsEntry is where the magic happens
var item = items[i].webkitGetAsEntry();
if (item)
traverseFileTree(item);
, false);
More info: https://protonet.info/blog/html5-experiment-drag-drop-of-folders/
9
Even 2 years later, IE and Firefox don't seem to be willing to implement this.
– Nicolas Raoul
Jul 3 '14 at 9:09
8
Now, for Firefox as well: stackoverflow.com/a/33431704/195216 It shows folder uploading via drag'n'drop and via dialog in chrome and firefox!
– dforce
Nov 5 '15 at 7:42
2
Edge supports this too.
– ZachB
Dec 9 '16 at 22:52
3
Important warning: The code in this answer is limited to 100 files in a given directory. See here : bugs.chromium.org/p/chromium/issues/detail?id=514087
– johnozbay
Mar 2 '18 at 6:26
1
@johnozbay it's unfortunate that more people picked up your important warning, and it's not necessarily a Chromium issue since the spec saysreadEntries
won't return all the entires in a directory. Based on the bug link your provided, I've written up a complete answer: stackoverflow.com/a/53058574/885922
– xlm
Oct 30 '18 at 23:42
|
show 7 more comments
It's now possible, thanks to Chrome >= 21.
function traverseFileTree(item, path) "";
if (item.isFile)
// Get file
item.file(function(file)
console.log("File:", path + file.name);
);
else if (item.isDirectory)
// Get folder contents
var dirReader = item.createReader();
dirReader.readEntries(function(entries)
for (var i=0; i<entries.length; i++)
traverseFileTree(entries[i], path + item.name + "/");
);
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
for (var i=0; i<items.length; i++)
// webkitGetAsEntry is where the magic happens
var item = items[i].webkitGetAsEntry();
if (item)
traverseFileTree(item);
, false);
More info: https://protonet.info/blog/html5-experiment-drag-drop-of-folders/
It's now possible, thanks to Chrome >= 21.
function traverseFileTree(item, path) "";
if (item.isFile)
// Get file
item.file(function(file)
console.log("File:", path + file.name);
);
else if (item.isDirectory)
// Get folder contents
var dirReader = item.createReader();
dirReader.readEntries(function(entries)
for (var i=0; i<entries.length; i++)
traverseFileTree(entries[i], path + item.name + "/");
);
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
for (var i=0; i<items.length; i++)
// webkitGetAsEntry is where the magic happens
var item = items[i].webkitGetAsEntry();
if (item)
traverseFileTree(item);
, false);
More info: https://protonet.info/blog/html5-experiment-drag-drop-of-folders/
edited Dec 9 '16 at 22:53
ZachB
5,63313365
5,63313365
answered Jul 10 '12 at 9:35
Christopher BlumChristopher Blum
75164
75164
9
Even 2 years later, IE and Firefox don't seem to be willing to implement this.
– Nicolas Raoul
Jul 3 '14 at 9:09
8
Now, for Firefox as well: stackoverflow.com/a/33431704/195216 It shows folder uploading via drag'n'drop and via dialog in chrome and firefox!
– dforce
Nov 5 '15 at 7:42
2
Edge supports this too.
– ZachB
Dec 9 '16 at 22:52
3
Important warning: The code in this answer is limited to 100 files in a given directory. See here : bugs.chromium.org/p/chromium/issues/detail?id=514087
– johnozbay
Mar 2 '18 at 6:26
1
@johnozbay it's unfortunate that more people picked up your important warning, and it's not necessarily a Chromium issue since the spec saysreadEntries
won't return all the entires in a directory. Based on the bug link your provided, I've written up a complete answer: stackoverflow.com/a/53058574/885922
– xlm
Oct 30 '18 at 23:42
|
show 7 more comments
9
Even 2 years later, IE and Firefox don't seem to be willing to implement this.
– Nicolas Raoul
Jul 3 '14 at 9:09
8
Now, for Firefox as well: stackoverflow.com/a/33431704/195216 It shows folder uploading via drag'n'drop and via dialog in chrome and firefox!
– dforce
Nov 5 '15 at 7:42
2
Edge supports this too.
– ZachB
Dec 9 '16 at 22:52
3
Important warning: The code in this answer is limited to 100 files in a given directory. See here : bugs.chromium.org/p/chromium/issues/detail?id=514087
– johnozbay
Mar 2 '18 at 6:26
1
@johnozbay it's unfortunate that more people picked up your important warning, and it's not necessarily a Chromium issue since the spec saysreadEntries
won't return all the entires in a directory. Based on the bug link your provided, I've written up a complete answer: stackoverflow.com/a/53058574/885922
– xlm
Oct 30 '18 at 23:42
9
9
Even 2 years later, IE and Firefox don't seem to be willing to implement this.
– Nicolas Raoul
Jul 3 '14 at 9:09
Even 2 years later, IE and Firefox don't seem to be willing to implement this.
– Nicolas Raoul
Jul 3 '14 at 9:09
8
8
Now, for Firefox as well: stackoverflow.com/a/33431704/195216 It shows folder uploading via drag'n'drop and via dialog in chrome and firefox!
– dforce
Nov 5 '15 at 7:42
Now, for Firefox as well: stackoverflow.com/a/33431704/195216 It shows folder uploading via drag'n'drop and via dialog in chrome and firefox!
– dforce
Nov 5 '15 at 7:42
2
2
Edge supports this too.
– ZachB
Dec 9 '16 at 22:52
Edge supports this too.
– ZachB
Dec 9 '16 at 22:52
3
3
Important warning: The code in this answer is limited to 100 files in a given directory. See here : bugs.chromium.org/p/chromium/issues/detail?id=514087
– johnozbay
Mar 2 '18 at 6:26
Important warning: The code in this answer is limited to 100 files in a given directory. See here : bugs.chromium.org/p/chromium/issues/detail?id=514087
– johnozbay
Mar 2 '18 at 6:26
1
1
@johnozbay it's unfortunate that more people picked up your important warning, and it's not necessarily a Chromium issue since the spec says
readEntries
won't return all the entires in a directory. Based on the bug link your provided, I've written up a complete answer: stackoverflow.com/a/53058574/885922– xlm
Oct 30 '18 at 23:42
@johnozbay it's unfortunate that more people picked up your important warning, and it's not necessarily a Chromium issue since the spec says
readEntries
won't return all the entires in a directory. Based on the bug link your provided, I've written up a complete answer: stackoverflow.com/a/53058574/885922– xlm
Oct 30 '18 at 23:42
|
show 7 more comments
In this message to the HTML 5 mailing list Ian Hickson says:
HTML5 now has to upload many files at
once. Browsers could allow users to
pick multiple files at once, including
across multiple directories; that's a
bit out of scope of the spec.
(Also see the original feature proposal.)
So it's safe to assume he considers uploading folders using drag-and-drop also out of scope. Apparently it's up to the browser to serve individual files.
Uploading folders would also have some other difficulties, as described by Lars Gunther:
This […] proposal must have two
checks (if it is doable at all):
Max size, to stop someone from uploading a full directory of several
hundred uncompressed raw images...
Filtering even if the accept attribute is omitted. Mac OS metadata
and Windows thumbnails, etc should be
omitted. All hidden files and
directories should default to be
excluded.
Hmmm, I agree on point 2... but only as long as there is a way for the web developer to determine if they want to enable the upload of hidden files - as there is always the potential that a hidden file could be operative to the use of the uploaded folder. Especially if the folder is a full on document split into multiple parts like a final cut file might be.
– Charles John Thompson III
Aug 6 '14 at 14:57
Disagree with out of scope: this is a cause of incompatibilities for something many people want to do, so it should be specified.
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:50
add a comment |
In this message to the HTML 5 mailing list Ian Hickson says:
HTML5 now has to upload many files at
once. Browsers could allow users to
pick multiple files at once, including
across multiple directories; that's a
bit out of scope of the spec.
(Also see the original feature proposal.)
So it's safe to assume he considers uploading folders using drag-and-drop also out of scope. Apparently it's up to the browser to serve individual files.
Uploading folders would also have some other difficulties, as described by Lars Gunther:
This […] proposal must have two
checks (if it is doable at all):
Max size, to stop someone from uploading a full directory of several
hundred uncompressed raw images...
Filtering even if the accept attribute is omitted. Mac OS metadata
and Windows thumbnails, etc should be
omitted. All hidden files and
directories should default to be
excluded.
Hmmm, I agree on point 2... but only as long as there is a way for the web developer to determine if they want to enable the upload of hidden files - as there is always the potential that a hidden file could be operative to the use of the uploaded folder. Especially if the folder is a full on document split into multiple parts like a final cut file might be.
– Charles John Thompson III
Aug 6 '14 at 14:57
Disagree with out of scope: this is a cause of incompatibilities for something many people want to do, so it should be specified.
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:50
add a comment |
In this message to the HTML 5 mailing list Ian Hickson says:
HTML5 now has to upload many files at
once. Browsers could allow users to
pick multiple files at once, including
across multiple directories; that's a
bit out of scope of the spec.
(Also see the original feature proposal.)
So it's safe to assume he considers uploading folders using drag-and-drop also out of scope. Apparently it's up to the browser to serve individual files.
Uploading folders would also have some other difficulties, as described by Lars Gunther:
This […] proposal must have two
checks (if it is doable at all):
Max size, to stop someone from uploading a full directory of several
hundred uncompressed raw images...
Filtering even if the accept attribute is omitted. Mac OS metadata
and Windows thumbnails, etc should be
omitted. All hidden files and
directories should default to be
excluded.
In this message to the HTML 5 mailing list Ian Hickson says:
HTML5 now has to upload many files at
once. Browsers could allow users to
pick multiple files at once, including
across multiple directories; that's a
bit out of scope of the spec.
(Also see the original feature proposal.)
So it's safe to assume he considers uploading folders using drag-and-drop also out of scope. Apparently it's up to the browser to serve individual files.
Uploading folders would also have some other difficulties, as described by Lars Gunther:
This […] proposal must have two
checks (if it is doable at all):
Max size, to stop someone from uploading a full directory of several
hundred uncompressed raw images...
Filtering even if the accept attribute is omitted. Mac OS metadata
and Windows thumbnails, etc should be
omitted. All hidden files and
directories should default to be
excluded.
answered Aug 28 '10 at 23:54
Marcel KorpelMarcel Korpel
19.4k45273
19.4k45273
Hmmm, I agree on point 2... but only as long as there is a way for the web developer to determine if they want to enable the upload of hidden files - as there is always the potential that a hidden file could be operative to the use of the uploaded folder. Especially if the folder is a full on document split into multiple parts like a final cut file might be.
– Charles John Thompson III
Aug 6 '14 at 14:57
Disagree with out of scope: this is a cause of incompatibilities for something many people want to do, so it should be specified.
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:50
add a comment |
Hmmm, I agree on point 2... but only as long as there is a way for the web developer to determine if they want to enable the upload of hidden files - as there is always the potential that a hidden file could be operative to the use of the uploaded folder. Especially if the folder is a full on document split into multiple parts like a final cut file might be.
– Charles John Thompson III
Aug 6 '14 at 14:57
Disagree with out of scope: this is a cause of incompatibilities for something many people want to do, so it should be specified.
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:50
Hmmm, I agree on point 2... but only as long as there is a way for the web developer to determine if they want to enable the upload of hidden files - as there is always the potential that a hidden file could be operative to the use of the uploaded folder. Especially if the folder is a full on document split into multiple parts like a final cut file might be.
– Charles John Thompson III
Aug 6 '14 at 14:57
Hmmm, I agree on point 2... but only as long as there is a way for the web developer to determine if they want to enable the upload of hidden files - as there is always the potential that a hidden file could be operative to the use of the uploaded folder. Especially if the folder is a full on document split into multiple parts like a final cut file might be.
– Charles John Thompson III
Aug 6 '14 at 14:57
Disagree with out of scope: this is a cause of incompatibilities for something many people want to do, so it should be specified.
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:50
Disagree with out of scope: this is a cause of incompatibilities for something many people want to do, so it should be specified.
– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:50
add a comment |
Now you can upload directories with both drag and drop and input.
<input type='file' webkitdirectory >
and for drag and drop(For webkit browsers).
Handling drag and drop folders.
<div id="dropzone"></div>
<script>
var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e)
var length = e.dataTransfer.items.length;
for (var i = 0; i < length; i++)
var entry = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry.isFile)
... // do whatever you want
else if (entry.isDirectory)
... // do whatever you want
;
</script>
Resources:
http://updates.html5rocks.com/2012/07/Drag-and-drop-a-folder-onto-Chrome-now-available
1
Is it possible to do the same for downloading without using compressed folders ?
– user2284570
Aug 28 '15 at 9:38
add a comment |
Now you can upload directories with both drag and drop and input.
<input type='file' webkitdirectory >
and for drag and drop(For webkit browsers).
Handling drag and drop folders.
<div id="dropzone"></div>
<script>
var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e)
var length = e.dataTransfer.items.length;
for (var i = 0; i < length; i++)
var entry = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry.isFile)
... // do whatever you want
else if (entry.isDirectory)
... // do whatever you want
;
</script>
Resources:
http://updates.html5rocks.com/2012/07/Drag-and-drop-a-folder-onto-Chrome-now-available
1
Is it possible to do the same for downloading without using compressed folders ?
– user2284570
Aug 28 '15 at 9:38
add a comment |
Now you can upload directories with both drag and drop and input.
<input type='file' webkitdirectory >
and for drag and drop(For webkit browsers).
Handling drag and drop folders.
<div id="dropzone"></div>
<script>
var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e)
var length = e.dataTransfer.items.length;
for (var i = 0; i < length; i++)
var entry = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry.isFile)
... // do whatever you want
else if (entry.isDirectory)
... // do whatever you want
;
</script>
Resources:
http://updates.html5rocks.com/2012/07/Drag-and-drop-a-folder-onto-Chrome-now-available
Now you can upload directories with both drag and drop and input.
<input type='file' webkitdirectory >
and for drag and drop(For webkit browsers).
Handling drag and drop folders.
<div id="dropzone"></div>
<script>
var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e)
var length = e.dataTransfer.items.length;
for (var i = 0; i < length; i++)
var entry = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry.isFile)
... // do whatever you want
else if (entry.isDirectory)
... // do whatever you want
;
</script>
Resources:
http://updates.html5rocks.com/2012/07/Drag-and-drop-a-folder-onto-Chrome-now-available
edited Feb 21 '13 at 13:19
andlrc
36.4k1265101
36.4k1265101
answered Jul 24 '12 at 12:24
Konga RajuKonga Raju
6,16074270
6,16074270
1
Is it possible to do the same for downloading without using compressed folders ?
– user2284570
Aug 28 '15 at 9:38
add a comment |
1
Is it possible to do the same for downloading without using compressed folders ?
– user2284570
Aug 28 '15 at 9:38
1
1
Is it possible to do the same for downloading without using compressed folders ?
– user2284570
Aug 28 '15 at 9:38
Is it possible to do the same for downloading without using compressed folders ?
– user2284570
Aug 28 '15 at 9:38
add a comment |
Firefox now supports folder upload, as of November 15, 2016, in v50.0: https://developer.mozilla.org/en-US/Firefox/Releases/50#Files_and_directories
You can drag and drop folders into Firefox or you can browse and select a local folder to upload. It also supports folders nested in subfolders.
That means you can now use either Chrome, Firefox, Edge or Opera to upload folders. You can't use Safari or Internet Explorer at present.
add a comment |
Firefox now supports folder upload, as of November 15, 2016, in v50.0: https://developer.mozilla.org/en-US/Firefox/Releases/50#Files_and_directories
You can drag and drop folders into Firefox or you can browse and select a local folder to upload. It also supports folders nested in subfolders.
That means you can now use either Chrome, Firefox, Edge or Opera to upload folders. You can't use Safari or Internet Explorer at present.
add a comment |
Firefox now supports folder upload, as of November 15, 2016, in v50.0: https://developer.mozilla.org/en-US/Firefox/Releases/50#Files_and_directories
You can drag and drop folders into Firefox or you can browse and select a local folder to upload. It also supports folders nested in subfolders.
That means you can now use either Chrome, Firefox, Edge or Opera to upload folders. You can't use Safari or Internet Explorer at present.
Firefox now supports folder upload, as of November 15, 2016, in v50.0: https://developer.mozilla.org/en-US/Firefox/Releases/50#Files_and_directories
You can drag and drop folders into Firefox or you can browse and select a local folder to upload. It also supports folders nested in subfolders.
That means you can now use either Chrome, Firefox, Edge or Opera to upload folders. You can't use Safari or Internet Explorer at present.
edited Jan 4 '17 at 12:17
answered Nov 19 '16 at 16:47
Magic ToolboxMagic Toolbox
19539
19539
add a comment |
add a comment |
This function will give you a promise for array of all dropped files, like <input type="file"/>.files
:
function getFilesWebkitDataTransferItems(dataTransferItems)
function traverseFileTreePromise(item, path='')
return new Promise( resolve =>
if (item.isFile)
item.file(file =>
file.filepath = path + file.name //save full path
files.push(file)
resolve(file)
)
else if (item.isDirectory)
let dirReader = item.createReader()
dirReader.readEntries(entries =>
let entriesPromises =
for (let entr of entries)
entriesPromises.push(traverseFileTreePromise(entr, path + item.name + "/"))
resolve(Promise.all(entriesPromises))
)
)
let files =
return new Promise((resolve, reject) =>
let entriesPromises =
for (let it of dataTransferItems)
entriesPromises.push(traverseFileTreePromise(it.webkitGetAsEntry()))
Promise.all(entriesPromises)
.then(entries =>
//console.log(entries)
resolve(files)
)
)
Usage:
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
getFilesFromWebkitDataTransferItems(items)
.then(files =>
...
)
, false);
npm package
https://www.npmjs.com/package/datatransfer-files-promise
usage example:
https://github.com/grabantot/datatransfer-files-promise/blob/master/index.html
4
This should be the new accepted answer. It is better than other answers because it returns a promise when complete. But there were a few mistakes:function getFilesWebkitDataTransferItems(dataTransfer)
should befunction getFilesWebkitDataTransferItems(items)
, andfor (entr of entries)
should befor (let entr of entries)
.
– RoccoB
Sep 3 '17 at 3:47
1
Won't actually get all the files in a directory (for Chrome it will only return 100 entries in a directory). Spec stipulates the need to callreadEntries
repeatedly until it returns an empty array.
– xlm
Oct 30 '18 at 23:28
@xlm Updated npm package. Now it handles >100 entries.
– grabantot
Nov 15 '18 at 16:01
add a comment |
This function will give you a promise for array of all dropped files, like <input type="file"/>.files
:
function getFilesWebkitDataTransferItems(dataTransferItems)
function traverseFileTreePromise(item, path='')
return new Promise( resolve =>
if (item.isFile)
item.file(file =>
file.filepath = path + file.name //save full path
files.push(file)
resolve(file)
)
else if (item.isDirectory)
let dirReader = item.createReader()
dirReader.readEntries(entries =>
let entriesPromises =
for (let entr of entries)
entriesPromises.push(traverseFileTreePromise(entr, path + item.name + "/"))
resolve(Promise.all(entriesPromises))
)
)
let files =
return new Promise((resolve, reject) =>
let entriesPromises =
for (let it of dataTransferItems)
entriesPromises.push(traverseFileTreePromise(it.webkitGetAsEntry()))
Promise.all(entriesPromises)
.then(entries =>
//console.log(entries)
resolve(files)
)
)
Usage:
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
getFilesFromWebkitDataTransferItems(items)
.then(files =>
...
)
, false);
npm package
https://www.npmjs.com/package/datatransfer-files-promise
usage example:
https://github.com/grabantot/datatransfer-files-promise/blob/master/index.html
4
This should be the new accepted answer. It is better than other answers because it returns a promise when complete. But there were a few mistakes:function getFilesWebkitDataTransferItems(dataTransfer)
should befunction getFilesWebkitDataTransferItems(items)
, andfor (entr of entries)
should befor (let entr of entries)
.
– RoccoB
Sep 3 '17 at 3:47
1
Won't actually get all the files in a directory (for Chrome it will only return 100 entries in a directory). Spec stipulates the need to callreadEntries
repeatedly until it returns an empty array.
– xlm
Oct 30 '18 at 23:28
@xlm Updated npm package. Now it handles >100 entries.
– grabantot
Nov 15 '18 at 16:01
add a comment |
This function will give you a promise for array of all dropped files, like <input type="file"/>.files
:
function getFilesWebkitDataTransferItems(dataTransferItems)
function traverseFileTreePromise(item, path='')
return new Promise( resolve =>
if (item.isFile)
item.file(file =>
file.filepath = path + file.name //save full path
files.push(file)
resolve(file)
)
else if (item.isDirectory)
let dirReader = item.createReader()
dirReader.readEntries(entries =>
let entriesPromises =
for (let entr of entries)
entriesPromises.push(traverseFileTreePromise(entr, path + item.name + "/"))
resolve(Promise.all(entriesPromises))
)
)
let files =
return new Promise((resolve, reject) =>
let entriesPromises =
for (let it of dataTransferItems)
entriesPromises.push(traverseFileTreePromise(it.webkitGetAsEntry()))
Promise.all(entriesPromises)
.then(entries =>
//console.log(entries)
resolve(files)
)
)
Usage:
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
getFilesFromWebkitDataTransferItems(items)
.then(files =>
...
)
, false);
npm package
https://www.npmjs.com/package/datatransfer-files-promise
usage example:
https://github.com/grabantot/datatransfer-files-promise/blob/master/index.html
This function will give you a promise for array of all dropped files, like <input type="file"/>.files
:
function getFilesWebkitDataTransferItems(dataTransferItems)
function traverseFileTreePromise(item, path='')
return new Promise( resolve =>
if (item.isFile)
item.file(file =>
file.filepath = path + file.name //save full path
files.push(file)
resolve(file)
)
else if (item.isDirectory)
let dirReader = item.createReader()
dirReader.readEntries(entries =>
let entriesPromises =
for (let entr of entries)
entriesPromises.push(traverseFileTreePromise(entr, path + item.name + "/"))
resolve(Promise.all(entriesPromises))
)
)
let files =
return new Promise((resolve, reject) =>
let entriesPromises =
for (let it of dataTransferItems)
entriesPromises.push(traverseFileTreePromise(it.webkitGetAsEntry()))
Promise.all(entriesPromises)
.then(entries =>
//console.log(entries)
resolve(files)
)
)
Usage:
dropArea.addEventListener("drop", function(event)
event.preventDefault();
var items = event.dataTransfer.items;
getFilesFromWebkitDataTransferItems(items)
.then(files =>
...
)
, false);
npm package
https://www.npmjs.com/package/datatransfer-files-promise
usage example:
https://github.com/grabantot/datatransfer-files-promise/blob/master/index.html
edited Nov 15 '18 at 16:03
answered Jul 5 '17 at 6:45
grabantotgrabantot
1,016818
1,016818
4
This should be the new accepted answer. It is better than other answers because it returns a promise when complete. But there were a few mistakes:function getFilesWebkitDataTransferItems(dataTransfer)
should befunction getFilesWebkitDataTransferItems(items)
, andfor (entr of entries)
should befor (let entr of entries)
.
– RoccoB
Sep 3 '17 at 3:47
1
Won't actually get all the files in a directory (for Chrome it will only return 100 entries in a directory). Spec stipulates the need to callreadEntries
repeatedly until it returns an empty array.
– xlm
Oct 30 '18 at 23:28
@xlm Updated npm package. Now it handles >100 entries.
– grabantot
Nov 15 '18 at 16:01
add a comment |
4
This should be the new accepted answer. It is better than other answers because it returns a promise when complete. But there were a few mistakes:function getFilesWebkitDataTransferItems(dataTransfer)
should befunction getFilesWebkitDataTransferItems(items)
, andfor (entr of entries)
should befor (let entr of entries)
.
– RoccoB
Sep 3 '17 at 3:47
1
Won't actually get all the files in a directory (for Chrome it will only return 100 entries in a directory). Spec stipulates the need to callreadEntries
repeatedly until it returns an empty array.
– xlm
Oct 30 '18 at 23:28
@xlm Updated npm package. Now it handles >100 entries.
– grabantot
Nov 15 '18 at 16:01
4
4
This should be the new accepted answer. It is better than other answers because it returns a promise when complete. But there were a few mistakes:
function getFilesWebkitDataTransferItems(dataTransfer)
should be function getFilesWebkitDataTransferItems(items)
, and for (entr of entries)
should be for (let entr of entries)
.– RoccoB
Sep 3 '17 at 3:47
This should be the new accepted answer. It is better than other answers because it returns a promise when complete. But there were a few mistakes:
function getFilesWebkitDataTransferItems(dataTransfer)
should be function getFilesWebkitDataTransferItems(items)
, and for (entr of entries)
should be for (let entr of entries)
.– RoccoB
Sep 3 '17 at 3:47
1
1
Won't actually get all the files in a directory (for Chrome it will only return 100 entries in a directory). Spec stipulates the need to call
readEntries
repeatedly until it returns an empty array.– xlm
Oct 30 '18 at 23:28
Won't actually get all the files in a directory (for Chrome it will only return 100 entries in a directory). Spec stipulates the need to call
readEntries
repeatedly until it returns an empty array.– xlm
Oct 30 '18 at 23:28
@xlm Updated npm package. Now it handles >100 entries.
– grabantot
Nov 15 '18 at 16:01
@xlm Updated npm package. Now it handles >100 entries.
– grabantot
Nov 15 '18 at 16:01
add a comment |
Unfortunately none of the existing answers are completely correct because readEntries
will not necessarily return ALL the (file or directory) entries for a given directory. This is part of the API specification (see Documentation section below).
To actually get all the files, we'll need to call readEntries
repeatedly (for each directory we encounter) until it returns an empty array. If we don't, we will miss some files/sub-directories in a directory e.g. in Chrome, readEntries
will only return at most 100 entries at a time.
Using Promises (await
/ async
) to more clearly demonstrate the correct usage of readEntries
(since it's asynchronous), and BFS to traverse the directory structure:
// Drop handler function to get all files
async function getAllFileEntries(dataTransferItemList)
let fileEntries = ;
// Use BFS to traverse entire directory/file structure
let queue = ;
// Unfortunately dataTransferItemList is not iterable i.e. no forEach
for (let i = 0; i < dataTransferItemList.length; i++)
queue.push(dataTransferItemList[i].webkitGetAsEntry());
while (queue.length > 0)
let entry = queue.shift();
if (entry.isFile)
fileEntries.push(entry);
else if (entry.isDirectory)
queue.push(...await readAllDirectoryEntries(entry.createReader()));
return fileEntries;
// Get all the entries (files or sub-directories) in a directory
// by calling readEntries until it returns empty array
async function readAllDirectoryEntries(directoryReader)
let entries = ;
let readEntries = await readEntriesPromise(directoryReader);
while (readEntries.length > 0)
entries.push(...readEntries);
readEntries = await readEntriesPromise(directoryReader);
return entries;
// Wrap readEntries in a promise to make working with readEntries easier
// readEntries will return only some of the entries in a directory
// e.g. Chrome returns at most 100 entries at a time
async function readEntriesPromise(directoryReader)
try
return await new Promise((resolve, reject) =>
directoryReader.readEntries(resolve, reject);
);
catch (err)
console.log(err);
Complete working example on Codepen: https://codepen.io/anon/pen/gBJrOP
FWIW I only picked this up because I wasn't getting back all the files I expected in a directory containing 40,000 files (many directories containing well over 100 files/sub-directories) when using the accepted answer.
Documentation:
This behaviour is documented in FileSystemDirectoryReader. Excerpt with emphasis added:
readEntries()
Returns a an array containing some number of the
directory's entries. Each item in the array is an object based on
FileSystemEntry—typically either FileSystemFileEntry or
FileSystemDirectoryEntry.
But to be fair, the MDN documentation could make this clearer in other sections. The readEntries() documentation simply notes:
readEntries() method retrieves the directory entries within the directory being read and delivers them in an array to the provided callback function
And the only mention/hint that multiple calls are needed is in the description of successCallback parameter:
If there are no files left, or you've already called readEntries() on
this FileSystemDirectoryReader, the array is empty.
Arguably the API could be more intuitive as well, but as the documentation notes: it is a non-standard/experimental feature, not on a standards track, and can't be expected to work for all browsers.
Related:
johnozbay comments that on Chrome,readEntries
will return at most 100 entries for a directory (verified as Chrome 64).
Xan explains the correct usage ofreadEntries
quite well in this answer (albeit without code).
Pablo Barría Urenda's answer correctly callsreadEntries
in a asynchronous manner without BFS. He also notes that Firefox returns all the entries in a directory (unlike Chrome) but we can't rely on this given the specification.
3
Thanks a lot for the shout-out, and getting this content out there. SOF needs more fantastic members like yourself! ✌🏻
– johnozbay
Nov 1 '18 at 10:48
3
I appreciate that @johnozbay I'm just concerned that it seems that many users are overlooking this small but significant fact re: specification/API and this edge case (100+ files in a directory) isn't that unlikely. I only realised it when I wasn't getting back all the files I expected. Your comment should have been answer.
– xlm
Nov 2 '18 at 2:43
add a comment |
Unfortunately none of the existing answers are completely correct because readEntries
will not necessarily return ALL the (file or directory) entries for a given directory. This is part of the API specification (see Documentation section below).
To actually get all the files, we'll need to call readEntries
repeatedly (for each directory we encounter) until it returns an empty array. If we don't, we will miss some files/sub-directories in a directory e.g. in Chrome, readEntries
will only return at most 100 entries at a time.
Using Promises (await
/ async
) to more clearly demonstrate the correct usage of readEntries
(since it's asynchronous), and BFS to traverse the directory structure:
// Drop handler function to get all files
async function getAllFileEntries(dataTransferItemList)
let fileEntries = ;
// Use BFS to traverse entire directory/file structure
let queue = ;
// Unfortunately dataTransferItemList is not iterable i.e. no forEach
for (let i = 0; i < dataTransferItemList.length; i++)
queue.push(dataTransferItemList[i].webkitGetAsEntry());
while (queue.length > 0)
let entry = queue.shift();
if (entry.isFile)
fileEntries.push(entry);
else if (entry.isDirectory)
queue.push(...await readAllDirectoryEntries(entry.createReader()));
return fileEntries;
// Get all the entries (files or sub-directories) in a directory
// by calling readEntries until it returns empty array
async function readAllDirectoryEntries(directoryReader)
let entries = ;
let readEntries = await readEntriesPromise(directoryReader);
while (readEntries.length > 0)
entries.push(...readEntries);
readEntries = await readEntriesPromise(directoryReader);
return entries;
// Wrap readEntries in a promise to make working with readEntries easier
// readEntries will return only some of the entries in a directory
// e.g. Chrome returns at most 100 entries at a time
async function readEntriesPromise(directoryReader)
try
return await new Promise((resolve, reject) =>
directoryReader.readEntries(resolve, reject);
);
catch (err)
console.log(err);
Complete working example on Codepen: https://codepen.io/anon/pen/gBJrOP
FWIW I only picked this up because I wasn't getting back all the files I expected in a directory containing 40,000 files (many directories containing well over 100 files/sub-directories) when using the accepted answer.
Documentation:
This behaviour is documented in FileSystemDirectoryReader. Excerpt with emphasis added:
readEntries()
Returns a an array containing some number of the
directory's entries. Each item in the array is an object based on
FileSystemEntry—typically either FileSystemFileEntry or
FileSystemDirectoryEntry.
But to be fair, the MDN documentation could make this clearer in other sections. The readEntries() documentation simply notes:
readEntries() method retrieves the directory entries within the directory being read and delivers them in an array to the provided callback function
And the only mention/hint that multiple calls are needed is in the description of successCallback parameter:
If there are no files left, or you've already called readEntries() on
this FileSystemDirectoryReader, the array is empty.
Arguably the API could be more intuitive as well, but as the documentation notes: it is a non-standard/experimental feature, not on a standards track, and can't be expected to work for all browsers.
Related:
johnozbay comments that on Chrome,readEntries
will return at most 100 entries for a directory (verified as Chrome 64).
Xan explains the correct usage ofreadEntries
quite well in this answer (albeit without code).
Pablo Barría Urenda's answer correctly callsreadEntries
in a asynchronous manner without BFS. He also notes that Firefox returns all the entries in a directory (unlike Chrome) but we can't rely on this given the specification.
3
Thanks a lot for the shout-out, and getting this content out there. SOF needs more fantastic members like yourself! ✌🏻
– johnozbay
Nov 1 '18 at 10:48
3
I appreciate that @johnozbay I'm just concerned that it seems that many users are overlooking this small but significant fact re: specification/API and this edge case (100+ files in a directory) isn't that unlikely. I only realised it when I wasn't getting back all the files I expected. Your comment should have been answer.
– xlm
Nov 2 '18 at 2:43
add a comment |
Unfortunately none of the existing answers are completely correct because readEntries
will not necessarily return ALL the (file or directory) entries for a given directory. This is part of the API specification (see Documentation section below).
To actually get all the files, we'll need to call readEntries
repeatedly (for each directory we encounter) until it returns an empty array. If we don't, we will miss some files/sub-directories in a directory e.g. in Chrome, readEntries
will only return at most 100 entries at a time.
Using Promises (await
/ async
) to more clearly demonstrate the correct usage of readEntries
(since it's asynchronous), and BFS to traverse the directory structure:
// Drop handler function to get all files
async function getAllFileEntries(dataTransferItemList)
let fileEntries = ;
// Use BFS to traverse entire directory/file structure
let queue = ;
// Unfortunately dataTransferItemList is not iterable i.e. no forEach
for (let i = 0; i < dataTransferItemList.length; i++)
queue.push(dataTransferItemList[i].webkitGetAsEntry());
while (queue.length > 0)
let entry = queue.shift();
if (entry.isFile)
fileEntries.push(entry);
else if (entry.isDirectory)
queue.push(...await readAllDirectoryEntries(entry.createReader()));
return fileEntries;
// Get all the entries (files or sub-directories) in a directory
// by calling readEntries until it returns empty array
async function readAllDirectoryEntries(directoryReader)
let entries = ;
let readEntries = await readEntriesPromise(directoryReader);
while (readEntries.length > 0)
entries.push(...readEntries);
readEntries = await readEntriesPromise(directoryReader);
return entries;
// Wrap readEntries in a promise to make working with readEntries easier
// readEntries will return only some of the entries in a directory
// e.g. Chrome returns at most 100 entries at a time
async function readEntriesPromise(directoryReader)
try
return await new Promise((resolve, reject) =>
directoryReader.readEntries(resolve, reject);
);
catch (err)
console.log(err);
Complete working example on Codepen: https://codepen.io/anon/pen/gBJrOP
FWIW I only picked this up because I wasn't getting back all the files I expected in a directory containing 40,000 files (many directories containing well over 100 files/sub-directories) when using the accepted answer.
Documentation:
This behaviour is documented in FileSystemDirectoryReader. Excerpt with emphasis added:
readEntries()
Returns a an array containing some number of the
directory's entries. Each item in the array is an object based on
FileSystemEntry—typically either FileSystemFileEntry or
FileSystemDirectoryEntry.
But to be fair, the MDN documentation could make this clearer in other sections. The readEntries() documentation simply notes:
readEntries() method retrieves the directory entries within the directory being read and delivers them in an array to the provided callback function
And the only mention/hint that multiple calls are needed is in the description of successCallback parameter:
If there are no files left, or you've already called readEntries() on
this FileSystemDirectoryReader, the array is empty.
Arguably the API could be more intuitive as well, but as the documentation notes: it is a non-standard/experimental feature, not on a standards track, and can't be expected to work for all browsers.
Related:
johnozbay comments that on Chrome,readEntries
will return at most 100 entries for a directory (verified as Chrome 64).
Xan explains the correct usage ofreadEntries
quite well in this answer (albeit without code).
Pablo Barría Urenda's answer correctly callsreadEntries
in a asynchronous manner without BFS. He also notes that Firefox returns all the entries in a directory (unlike Chrome) but we can't rely on this given the specification.
Unfortunately none of the existing answers are completely correct because readEntries
will not necessarily return ALL the (file or directory) entries for a given directory. This is part of the API specification (see Documentation section below).
To actually get all the files, we'll need to call readEntries
repeatedly (for each directory we encounter) until it returns an empty array. If we don't, we will miss some files/sub-directories in a directory e.g. in Chrome, readEntries
will only return at most 100 entries at a time.
Using Promises (await
/ async
) to more clearly demonstrate the correct usage of readEntries
(since it's asynchronous), and BFS to traverse the directory structure:
// Drop handler function to get all files
async function getAllFileEntries(dataTransferItemList)
let fileEntries = ;
// Use BFS to traverse entire directory/file structure
let queue = ;
// Unfortunately dataTransferItemList is not iterable i.e. no forEach
for (let i = 0; i < dataTransferItemList.length; i++)
queue.push(dataTransferItemList[i].webkitGetAsEntry());
while (queue.length > 0)
let entry = queue.shift();
if (entry.isFile)
fileEntries.push(entry);
else if (entry.isDirectory)
queue.push(...await readAllDirectoryEntries(entry.createReader()));
return fileEntries;
// Get all the entries (files or sub-directories) in a directory
// by calling readEntries until it returns empty array
async function readAllDirectoryEntries(directoryReader)
let entries = ;
let readEntries = await readEntriesPromise(directoryReader);
while (readEntries.length > 0)
entries.push(...readEntries);
readEntries = await readEntriesPromise(directoryReader);
return entries;
// Wrap readEntries in a promise to make working with readEntries easier
// readEntries will return only some of the entries in a directory
// e.g. Chrome returns at most 100 entries at a time
async function readEntriesPromise(directoryReader)
try
return await new Promise((resolve, reject) =>
directoryReader.readEntries(resolve, reject);
);
catch (err)
console.log(err);
Complete working example on Codepen: https://codepen.io/anon/pen/gBJrOP
FWIW I only picked this up because I wasn't getting back all the files I expected in a directory containing 40,000 files (many directories containing well over 100 files/sub-directories) when using the accepted answer.
Documentation:
This behaviour is documented in FileSystemDirectoryReader. Excerpt with emphasis added:
readEntries()
Returns a an array containing some number of the
directory's entries. Each item in the array is an object based on
FileSystemEntry—typically either FileSystemFileEntry or
FileSystemDirectoryEntry.
But to be fair, the MDN documentation could make this clearer in other sections. The readEntries() documentation simply notes:
readEntries() method retrieves the directory entries within the directory being read and delivers them in an array to the provided callback function
And the only mention/hint that multiple calls are needed is in the description of successCallback parameter:
If there are no files left, or you've already called readEntries() on
this FileSystemDirectoryReader, the array is empty.
Arguably the API could be more intuitive as well, but as the documentation notes: it is a non-standard/experimental feature, not on a standards track, and can't be expected to work for all browsers.
Related:
johnozbay comments that on Chrome,readEntries
will return at most 100 entries for a directory (verified as Chrome 64).
Xan explains the correct usage ofreadEntries
quite well in this answer (albeit without code).
Pablo Barría Urenda's answer correctly callsreadEntries
in a asynchronous manner without BFS. He also notes that Firefox returns all the entries in a directory (unlike Chrome) but we can't rely on this given the specification.
edited Nov 2 '18 at 5:04
answered Oct 30 '18 at 6:31
xlmxlm
3,18493440
3,18493440
3
Thanks a lot for the shout-out, and getting this content out there. SOF needs more fantastic members like yourself! ✌🏻
– johnozbay
Nov 1 '18 at 10:48
3
I appreciate that @johnozbay I'm just concerned that it seems that many users are overlooking this small but significant fact re: specification/API and this edge case (100+ files in a directory) isn't that unlikely. I only realised it when I wasn't getting back all the files I expected. Your comment should have been answer.
– xlm
Nov 2 '18 at 2:43
add a comment |
3
Thanks a lot for the shout-out, and getting this content out there. SOF needs more fantastic members like yourself! ✌🏻
– johnozbay
Nov 1 '18 at 10:48
3
I appreciate that @johnozbay I'm just concerned that it seems that many users are overlooking this small but significant fact re: specification/API and this edge case (100+ files in a directory) isn't that unlikely. I only realised it when I wasn't getting back all the files I expected. Your comment should have been answer.
– xlm
Nov 2 '18 at 2:43
3
3
Thanks a lot for the shout-out, and getting this content out there. SOF needs more fantastic members like yourself! ✌🏻
– johnozbay
Nov 1 '18 at 10:48
Thanks a lot for the shout-out, and getting this content out there. SOF needs more fantastic members like yourself! ✌🏻
– johnozbay
Nov 1 '18 at 10:48
3
3
I appreciate that @johnozbay I'm just concerned that it seems that many users are overlooking this small but significant fact re: specification/API and this edge case (100+ files in a directory) isn't that unlikely. I only realised it when I wasn't getting back all the files I expected. Your comment should have been answer.
– xlm
Nov 2 '18 at 2:43
I appreciate that @johnozbay I'm just concerned that it seems that many users are overlooking this small but significant fact re: specification/API and this edge case (100+ files in a directory) isn't that unlikely. I only realised it when I wasn't getting back all the files I expected. Your comment should have been answer.
– xlm
Nov 2 '18 at 2:43
add a comment |
The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively.
Actually, in Chrome/Chromium, you can upload a folder, but when you do it, it just uploads a meaningless 4KB file, which represents the directory. Some servers-side applications like Alfresco can detect this, and warn the user that folders can not be uploaded:
A pointless file, or a pointer to a file?
– MoB
Aug 6 '12 at 9:02
@MoB: maybe it is some kind of pointer indeed. But since the actual file is on the client machine, the server machine will not be able to do anything with this pointer, of course.
– Nicolas Raoul
Aug 6 '12 at 10:37
add a comment |
The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively.
Actually, in Chrome/Chromium, you can upload a folder, but when you do it, it just uploads a meaningless 4KB file, which represents the directory. Some servers-side applications like Alfresco can detect this, and warn the user that folders can not be uploaded:
A pointless file, or a pointer to a file?
– MoB
Aug 6 '12 at 9:02
@MoB: maybe it is some kind of pointer indeed. But since the actual file is on the client machine, the server machine will not be able to do anything with this pointer, of course.
– Nicolas Raoul
Aug 6 '12 at 10:37
add a comment |
The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively.
Actually, in Chrome/Chromium, you can upload a folder, but when you do it, it just uploads a meaningless 4KB file, which represents the directory. Some servers-side applications like Alfresco can detect this, and warn the user that folders can not be uploaded:
The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively.
Actually, in Chrome/Chromium, you can upload a folder, but when you do it, it just uploads a meaningless 4KB file, which represents the directory. Some servers-side applications like Alfresco can detect this, and warn the user that folders can not be uploaded:
answered Jun 12 '12 at 4:42
Nicolas RaoulNicolas Raoul
34.3k47163293
34.3k47163293
A pointless file, or a pointer to a file?
– MoB
Aug 6 '12 at 9:02
@MoB: maybe it is some kind of pointer indeed. But since the actual file is on the client machine, the server machine will not be able to do anything with this pointer, of course.
– Nicolas Raoul
Aug 6 '12 at 10:37
add a comment |
A pointless file, or a pointer to a file?
– MoB
Aug 6 '12 at 9:02
@MoB: maybe it is some kind of pointer indeed. But since the actual file is on the client machine, the server machine will not be able to do anything with this pointer, of course.
– Nicolas Raoul
Aug 6 '12 at 10:37
A pointless file, or a pointer to a file?
– MoB
Aug 6 '12 at 9:02
A pointless file, or a pointer to a file?
– MoB
Aug 6 '12 at 9:02
@MoB: maybe it is some kind of pointer indeed. But since the actual file is on the client machine, the server machine will not be able to do anything with this pointer, of course.
– Nicolas Raoul
Aug 6 '12 at 10:37
@MoB: maybe it is some kind of pointer indeed. But since the actual file is on the client machine, the server machine will not be able to do anything with this pointer, of course.
– Nicolas Raoul
Aug 6 '12 at 10:37
add a comment |
Here's a complete example of how to use the file and directory entries API:
var dropzone = document.getElementById("dropzone");
var listing = document.getElementById("listing");
function scanAndLogFiles(item, container)
var elem = document.createElement("li");
elem.innerHTML = item.name;
container.appendChild(elem);
if (item.isDirectory)
var directoryReader = item.createReader();
var directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries)
entries.forEach(function(entry)
scanAndLogFiles(entry, directoryContainer);
);
);
dropzone.addEventListener(
"dragover",
function(event)
event.preventDefault();
,
false
);
dropzone.addEventListener(
"drop",
function(event)
var items = event.dataTransfer.items;
event.preventDefault();
listing.innerHTML = "";
for (var i = 0; i < items.length; i++)
var item = items[i].webkitGetAsEntry();
if (item)
scanAndLogFiles(item, listing);
,
false
);
body
font: 14px "Arial", sans-serif;
#dropzone
text-align: center;
width: 300px;
height: 100px;
margin: 10px;
padding: 10px;
border: 4px dashed red;
border-radius: 10px;
#boxtitle
display: table-cell;
vertical-align: middle;
text-align: center;
color: black;
font: bold 2em "Arial", sans-serif;
width: 300px;
height: 100px;
<p>Drag files and/or directories to the box below!</p>
<div id="dropzone">
<div id="boxtitle">
Drop Files Here
</div>
</div>
<h2>Directory tree:</h2>
<ul id="listing"></ul>
webkitGetAsEntry
is supported by Chrome 13+, Firefox 50+ and Edge.
Source: https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry
add a comment |
Here's a complete example of how to use the file and directory entries API:
var dropzone = document.getElementById("dropzone");
var listing = document.getElementById("listing");
function scanAndLogFiles(item, container)
var elem = document.createElement("li");
elem.innerHTML = item.name;
container.appendChild(elem);
if (item.isDirectory)
var directoryReader = item.createReader();
var directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries)
entries.forEach(function(entry)
scanAndLogFiles(entry, directoryContainer);
);
);
dropzone.addEventListener(
"dragover",
function(event)
event.preventDefault();
,
false
);
dropzone.addEventListener(
"drop",
function(event)
var items = event.dataTransfer.items;
event.preventDefault();
listing.innerHTML = "";
for (var i = 0; i < items.length; i++)
var item = items[i].webkitGetAsEntry();
if (item)
scanAndLogFiles(item, listing);
,
false
);
body
font: 14px "Arial", sans-serif;
#dropzone
text-align: center;
width: 300px;
height: 100px;
margin: 10px;
padding: 10px;
border: 4px dashed red;
border-radius: 10px;
#boxtitle
display: table-cell;
vertical-align: middle;
text-align: center;
color: black;
font: bold 2em "Arial", sans-serif;
width: 300px;
height: 100px;
<p>Drag files and/or directories to the box below!</p>
<div id="dropzone">
<div id="boxtitle">
Drop Files Here
</div>
</div>
<h2>Directory tree:</h2>
<ul id="listing"></ul>
webkitGetAsEntry
is supported by Chrome 13+, Firefox 50+ and Edge.
Source: https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry
add a comment |
Here's a complete example of how to use the file and directory entries API:
var dropzone = document.getElementById("dropzone");
var listing = document.getElementById("listing");
function scanAndLogFiles(item, container)
var elem = document.createElement("li");
elem.innerHTML = item.name;
container.appendChild(elem);
if (item.isDirectory)
var directoryReader = item.createReader();
var directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries)
entries.forEach(function(entry)
scanAndLogFiles(entry, directoryContainer);
);
);
dropzone.addEventListener(
"dragover",
function(event)
event.preventDefault();
,
false
);
dropzone.addEventListener(
"drop",
function(event)
var items = event.dataTransfer.items;
event.preventDefault();
listing.innerHTML = "";
for (var i = 0; i < items.length; i++)
var item = items[i].webkitGetAsEntry();
if (item)
scanAndLogFiles(item, listing);
,
false
);
body
font: 14px "Arial", sans-serif;
#dropzone
text-align: center;
width: 300px;
height: 100px;
margin: 10px;
padding: 10px;
border: 4px dashed red;
border-radius: 10px;
#boxtitle
display: table-cell;
vertical-align: middle;
text-align: center;
color: black;
font: bold 2em "Arial", sans-serif;
width: 300px;
height: 100px;
<p>Drag files and/or directories to the box below!</p>
<div id="dropzone">
<div id="boxtitle">
Drop Files Here
</div>
</div>
<h2>Directory tree:</h2>
<ul id="listing"></ul>
webkitGetAsEntry
is supported by Chrome 13+, Firefox 50+ and Edge.
Source: https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry
Here's a complete example of how to use the file and directory entries API:
var dropzone = document.getElementById("dropzone");
var listing = document.getElementById("listing");
function scanAndLogFiles(item, container)
var elem = document.createElement("li");
elem.innerHTML = item.name;
container.appendChild(elem);
if (item.isDirectory)
var directoryReader = item.createReader();
var directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries)
entries.forEach(function(entry)
scanAndLogFiles(entry, directoryContainer);
);
);
dropzone.addEventListener(
"dragover",
function(event)
event.preventDefault();
,
false
);
dropzone.addEventListener(
"drop",
function(event)
var items = event.dataTransfer.items;
event.preventDefault();
listing.innerHTML = "";
for (var i = 0; i < items.length; i++)
var item = items[i].webkitGetAsEntry();
if (item)
scanAndLogFiles(item, listing);
,
false
);
body
font: 14px "Arial", sans-serif;
#dropzone
text-align: center;
width: 300px;
height: 100px;
margin: 10px;
padding: 10px;
border: 4px dashed red;
border-radius: 10px;
#boxtitle
display: table-cell;
vertical-align: middle;
text-align: center;
color: black;
font: bold 2em "Arial", sans-serif;
width: 300px;
height: 100px;
<p>Drag files and/or directories to the box below!</p>
<div id="dropzone">
<div id="boxtitle">
Drop Files Here
</div>
</div>
<h2>Directory tree:</h2>
<ul id="listing"></ul>
webkitGetAsEntry
is supported by Chrome 13+, Firefox 50+ and Edge.
Source: https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry
var dropzone = document.getElementById("dropzone");
var listing = document.getElementById("listing");
function scanAndLogFiles(item, container)
var elem = document.createElement("li");
elem.innerHTML = item.name;
container.appendChild(elem);
if (item.isDirectory)
var directoryReader = item.createReader();
var directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries)
entries.forEach(function(entry)
scanAndLogFiles(entry, directoryContainer);
);
);
dropzone.addEventListener(
"dragover",
function(event)
event.preventDefault();
,
false
);
dropzone.addEventListener(
"drop",
function(event)
var items = event.dataTransfer.items;
event.preventDefault();
listing.innerHTML = "";
for (var i = 0; i < items.length; i++)
var item = items[i].webkitGetAsEntry();
if (item)
scanAndLogFiles(item, listing);
,
false
);
body
font: 14px "Arial", sans-serif;
#dropzone
text-align: center;
width: 300px;
height: 100px;
margin: 10px;
padding: 10px;
border: 4px dashed red;
border-radius: 10px;
#boxtitle
display: table-cell;
vertical-align: middle;
text-align: center;
color: black;
font: bold 2em "Arial", sans-serif;
width: 300px;
height: 100px;
<p>Drag files and/or directories to the box below!</p>
<div id="dropzone">
<div id="boxtitle">
Drop Files Here
</div>
</div>
<h2>Directory tree:</h2>
<ul id="listing"></ul>
var dropzone = document.getElementById("dropzone");
var listing = document.getElementById("listing");
function scanAndLogFiles(item, container)
var elem = document.createElement("li");
elem.innerHTML = item.name;
container.appendChild(elem);
if (item.isDirectory)
var directoryReader = item.createReader();
var directoryContainer = document.createElement("ul");
container.appendChild(directoryContainer);
directoryReader.readEntries(function(entries)
entries.forEach(function(entry)
scanAndLogFiles(entry, directoryContainer);
);
);
dropzone.addEventListener(
"dragover",
function(event)
event.preventDefault();
,
false
);
dropzone.addEventListener(
"drop",
function(event)
var items = event.dataTransfer.items;
event.preventDefault();
listing.innerHTML = "";
for (var i = 0; i < items.length; i++)
var item = items[i].webkitGetAsEntry();
if (item)
scanAndLogFiles(item, listing);
,
false
);
body
font: 14px "Arial", sans-serif;
#dropzone
text-align: center;
width: 300px;
height: 100px;
margin: 10px;
padding: 10px;
border: 4px dashed red;
border-radius: 10px;
#boxtitle
display: table-cell;
vertical-align: middle;
text-align: center;
color: black;
font: bold 2em "Arial", sans-serif;
width: 300px;
height: 100px;
<p>Drag files and/or directories to the box below!</p>
<div id="dropzone">
<div id="boxtitle">
Drop Files Here
</div>
</div>
<h2>Directory tree:</h2>
<ul id="listing"></ul>
answered Sep 14 '18 at 13:23
Paolo MorettiPaolo Moretti
37.9k188985
37.9k188985
add a comment |
add a comment |
Does HTML5 allow drag-drop upload of folders or a folder tree?
Only Chrome supports this feature. It has failed to have any traction and is likely to be removed.
Ref : https://developer.mozilla.org/en/docs/Web/API/DirectoryReader#readEntries
Wow. Telling from the W3C Note at that link, this is indeed not continued. What is the basis of the assumption that it has failed to get any traction?
– bebbi
Feb 29 '16 at 12:58
@bebbi no other browser vendors implemented it
– basarat
Feb 29 '16 at 23:23
1
@PabloBarríaUrenda comment is not true; his issue is likely referring to his question: stackoverflow.com/questions/51850469/… which he solved/realisedreadEntries
can't be called if another call ofreadEntries
is still being run. The DirectoryReader API design isn't the best
– xlm
Oct 30 '18 at 23:37
@xlm yes, indeed you are correct. I had posted this while I myself was puzzled by the issue, but I eventually resolved it (and forgot about this comment). I have now deleted the confusing comment.
– Pablo Barría Urenda
Oct 31 '18 at 7:42
add a comment |
Does HTML5 allow drag-drop upload of folders or a folder tree?
Only Chrome supports this feature. It has failed to have any traction and is likely to be removed.
Ref : https://developer.mozilla.org/en/docs/Web/API/DirectoryReader#readEntries
Wow. Telling from the W3C Note at that link, this is indeed not continued. What is the basis of the assumption that it has failed to get any traction?
– bebbi
Feb 29 '16 at 12:58
@bebbi no other browser vendors implemented it
– basarat
Feb 29 '16 at 23:23
1
@PabloBarríaUrenda comment is not true; his issue is likely referring to his question: stackoverflow.com/questions/51850469/… which he solved/realisedreadEntries
can't be called if another call ofreadEntries
is still being run. The DirectoryReader API design isn't the best
– xlm
Oct 30 '18 at 23:37
@xlm yes, indeed you are correct. I had posted this while I myself was puzzled by the issue, but I eventually resolved it (and forgot about this comment). I have now deleted the confusing comment.
– Pablo Barría Urenda
Oct 31 '18 at 7:42
add a comment |
Does HTML5 allow drag-drop upload of folders or a folder tree?
Only Chrome supports this feature. It has failed to have any traction and is likely to be removed.
Ref : https://developer.mozilla.org/en/docs/Web/API/DirectoryReader#readEntries
Does HTML5 allow drag-drop upload of folders or a folder tree?
Only Chrome supports this feature. It has failed to have any traction and is likely to be removed.
Ref : https://developer.mozilla.org/en/docs/Web/API/DirectoryReader#readEntries
answered Feb 3 '16 at 13:25
basaratbasarat
143k27268375
143k27268375
Wow. Telling from the W3C Note at that link, this is indeed not continued. What is the basis of the assumption that it has failed to get any traction?
– bebbi
Feb 29 '16 at 12:58
@bebbi no other browser vendors implemented it
– basarat
Feb 29 '16 at 23:23
1
@PabloBarríaUrenda comment is not true; his issue is likely referring to his question: stackoverflow.com/questions/51850469/… which he solved/realisedreadEntries
can't be called if another call ofreadEntries
is still being run. The DirectoryReader API design isn't the best
– xlm
Oct 30 '18 at 23:37
@xlm yes, indeed you are correct. I had posted this while I myself was puzzled by the issue, but I eventually resolved it (and forgot about this comment). I have now deleted the confusing comment.
– Pablo Barría Urenda
Oct 31 '18 at 7:42
add a comment |
Wow. Telling from the W3C Note at that link, this is indeed not continued. What is the basis of the assumption that it has failed to get any traction?
– bebbi
Feb 29 '16 at 12:58
@bebbi no other browser vendors implemented it
– basarat
Feb 29 '16 at 23:23
1
@PabloBarríaUrenda comment is not true; his issue is likely referring to his question: stackoverflow.com/questions/51850469/… which he solved/realisedreadEntries
can't be called if another call ofreadEntries
is still being run. The DirectoryReader API design isn't the best
– xlm
Oct 30 '18 at 23:37
@xlm yes, indeed you are correct. I had posted this while I myself was puzzled by the issue, but I eventually resolved it (and forgot about this comment). I have now deleted the confusing comment.
– Pablo Barría Urenda
Oct 31 '18 at 7:42
Wow. Telling from the W3C Note at that link, this is indeed not continued. What is the basis of the assumption that it has failed to get any traction?
– bebbi
Feb 29 '16 at 12:58
Wow. Telling from the W3C Note at that link, this is indeed not continued. What is the basis of the assumption that it has failed to get any traction?
– bebbi
Feb 29 '16 at 12:58
@bebbi no other browser vendors implemented it
– basarat
Feb 29 '16 at 23:23
@bebbi no other browser vendors implemented it
– basarat
Feb 29 '16 at 23:23
1
1
@PabloBarríaUrenda comment is not true; his issue is likely referring to his question: stackoverflow.com/questions/51850469/… which he solved/realised
readEntries
can't be called if another call of readEntries
is still being run. The DirectoryReader API design isn't the best– xlm
Oct 30 '18 at 23:37
@PabloBarríaUrenda comment is not true; his issue is likely referring to his question: stackoverflow.com/questions/51850469/… which he solved/realised
readEntries
can't be called if another call of readEntries
is still being run. The DirectoryReader API design isn't the best– xlm
Oct 30 '18 at 23:37
@xlm yes, indeed you are correct. I had posted this while I myself was puzzled by the issue, but I eventually resolved it (and forgot about this comment). I have now deleted the confusing comment.
– Pablo Barría Urenda
Oct 31 '18 at 7:42
@xlm yes, indeed you are correct. I had posted this while I myself was puzzled by the issue, but I eventually resolved it (and forgot about this comment). I have now deleted the confusing comment.
– Pablo Barría Urenda
Oct 31 '18 at 7:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3590058%2fdoes-html5-allow-drag-drop-upload-of-folders-or-a-folder-tree%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Same for
input type=file
: stackoverflow.com/questions/9518335/…– Ciro Santilli 新疆改造中心996ICU六四事件
Aug 23 '14 at 18:52
npm package npmjs.com/package/datatransfer-files-promise
– grabantot
Oct 20 '18 at 19:56