How to distinguish a file from a folder during upload using drag and drop in jquery?

If a user tries to drag a folder into a file loader control to download it, I need to show the user an error message that can only be downloaded for files. The problem is that I could not distinguish the file from the folder.

One way I thought was to check the jQuery file type property. Suppose the file name is "test.txt", then the file type will return "text / plain". For a regular folder name, such as "TestFolder", the file type will be empty and its size will return 0. However, if the folder name included an extension of the type "TestFolder.txt", then the file type would return "text / plain".

So, I could check the file size and file size, but it will not work correctly for the folder name, for example "TestFolder.txt". Can someone suggest me a good solution to fix this using jQuery or other methods?

+8
javascript jquery file-upload drag-and-drop
source share
3 answers

The ability to determine if a folder has been deleted depends on the support of the file system API user agent. If the user agent supports the file system API (currently only for Chrome 21+), you can use the Entry interface, which has two child interfaces: DirectoryEntry and FileEntry . The interface itself has the functions isDirectory and isFile . Without support for this interface, it is not possible to determine whether dropped items were folders when viewing a DataTransfer object.

+3
source share

Since you are not going to allow dragging and dropping the folder, first check if it is in the folder or file, second only if it is not a folder, and then check the file extension. But IE <11 does not support file APIs for processing. Hope this helps.

0
source share

As far as I know, the browser (and in fact javascript) does not have access to any file access methods for security purposes, so you cannot check what the file really is.

I worked with this problem myself in the past, and the best option I found is to process the file server side and return an error to the page after the download is complete.

I would be happy to see the best alternatives, at least.

-one
source share

All Articles