TL; DR you cannot :(
If you are wondering why this question has not yet received the accepted answer, you can read this meta-question created by OP, and my answer .
HTML5 drag / drop file
I did some research in many documents on this topic and tested them myself in different browsers, so I decided to summarize everything that I know about dragging and dropping files here.
Drag and drop
When dragging a file, you can use some listeners, for example:
dragenterdragoverdragenddragleave
Given that these are drag events, the files event.dataTransfer property will either have length == 0 or be null .
You cannot read file data in a drag event, and you cannot check if they are folders. This is not a mistake, it is a safety measure.
Imagine that you can read files during a drag event: you can read everything, even if the user does not want to upload files to your site. That would not make sense, seriously. Imagine you are dragging a file from your desktop to another folder and you accidentally drag it through a web page: now the web page reads your file and saves your personal information on its server ... which would be a huge security flaw.
However, you can still determine whether the user drags files (as well as files, which I also mean folders, because folders are files) or not, iterating over the array event.dataTransfer.types . You can create a function that checks if the drag event contains files, and then calls it in the event handler.
Example:
function containsFiles(event) { if (event.dataTransfer.types) { for (var i=0; i<event.dataTransfer.types.length; i++) { if (event.dataTransfer.types[i] == "Files") { return true; } } } return false; } function handleDragEnter(e) { e.preventDefault(); if (containsFiles(e)) {
Delete
When you drop the file into drop <div> (or any element that you use as dropzone), you will use the listener for the drop event to read some file properties, such as name, size, type and last modified date.
To determine if a file is a folder, you should:
- Check if the file is
type == "" because the folders are not of type. - Check if the file size is a multiple of 4096:
size%4096 == 0 , because folders always have a size that is a multiple of 4096 bytes (this is 4KiB).
Example:
function handleDrop(e) { e.stopPropagation(); e.preventDefault(); var files = e.dataTransfer.files; for (var i = 0, f; f = files[i]; i++) {
KNOWN ISSUE:. Since these folders are actually files, this is the only way to distinguish them from another type of file. Although this method does not give you complete confidence that the file is a folder: it can be a file without an extension and with a size of 0 or exactly N x 4096B.
Working examples
Here are some working examples to see what I said above and test it yourself. Before starting them, make sure your browser supports drag and drop . Good luck: