Problem
I have following code snippet that is used to get file information during file drag and drop upload:
var files = event.dataTransfer.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
// I need notDirectory(file) function.
notDirectory(file).then(function(file) {
output.innerHTML +=
`<p>
Name: ${file.name}</br>
Size: ${file.size} bytes</br>
Type: ${file.type}</br>
Modified Date: ${file.lastModifiedDate}
</p>`;
});
}
I did research and found that Firefox does not support directory uploads, but allows client to drag and drop them to drop area.
Question
How can I filter out directories from upload handler in Firefox?
Update
You can find working sample here: https://jsfiddle.net/gevorgha/exs3ta25/
Please consider that I need it to work on the latest stable Firefox version - 46.0.1 without enabling extra preferences from browser, because I don't want to ask users to enable preference to make upload work properly.
Directory upload was not enabled by default at firefox 47, where tried
html
,javascript
at stacksnippets, jsfiddle.See Firefox 42 for developers Interfaces/APIs/DOM
A workaround to detect folder upload could include
<input type="file">
withdirectory
andallowdirs
attributes set, possibly includingmultiple
attribute, see Note, for drag and drop or user selection on click of container;prefs.js
orabout:config
and setdom.input.dirpicker
preference toBoolean
true
;Directory
and not a singleFile
object, useif
with condition(filesAndDirs[0] && filesAndDirs[0].constructor.name === "Directory")
or(filesAndDirs[0] instanceof Directory)
inside of.then(function(filesAndDirectories){})
at chained to.getFilesAndDirectories()
;<label>
element for<div>
element as parent of<input type="file">
. Adjustcss
ofinput type="file"
to fill parent droppable container and setopacity
to0
; adjustcss
at parent element ofinput type="file"
to display text at:before
pseudo-element, overinput type="file"
child element.See also New API for directory picking and drag-and-drop.
Note, approach was tried at firefox 47, where both directories and individual files were successfully uploaded.
jsfiddle https://jsfiddle.net/exs3ta25/31/
You can read a little about this in here (including Firefix). Here is a function (not made by me but I edited maybe a half of it) that detects whether drop is file or folder and shows its name, size and what type (tested in FireFox):
I hope that's what you were looking for. Good luck!
Solution
I came up with following dirty workaround that works on Firefox version - 46.0.1
It uses FileReader API to check if uploaded file is directory or not.
Code
Related Links