How can you detect that a user has dragged a folder (not a file) into his browser?

I know very well that you are not allowed to load an entire folder using drag and drop. The problem is, how can I tell if someone is trying to do this? If you drag a folder into the browser, it will behave exactly as if you were dragging a file with some webkit extension that you don’t know about, for example, ".sh". How can you tell the difference?

I tested this on Chrome and Safari and Firefox on Mac OS X and Windows. Depending on the browser and OS, I get slightly different results. Sometimes it manages to load a file with a zero byte. Sometimes it uploads a folder image. Sometimes it cannot load anything.

event.dataTransfer.types and event.dataTransfer.items are both false and say that the type is "File" or "file", respectively.

Firefox provides this list of types:

{"0":"application/x-moz-file","1":"text/x-moz-url","2":"text/plain","3":"Files"} 
+4
source share
3 answers

I found a way to do this. Information can be found in dataTransfer.items via getAsEntry, although it is slightly different in different browsers. In addition, it does not have a pointer to the file you need, although you can understand it based on the file name, assuming that you are not simultaneously downloading two files with the same name. There is not much work to do, so this is the best I could do.

 just_the_files = (dataTransfer) -> real_files_set = {} for item in dataTransfer.items entry = item.getAsEntry?() or item.webkitGetAsEntry?() or item if entry.isFile real_files_set[entry.name] = true (file for file in dataTransfer.files when file.name of real_files_set) 
+1
source

In fact, when you drag and drop a folder into the browser, all folders and files that are in the folder that you drag will be shown.

for example in chrome

it will display "Index of D: \ my documents \ Downloads \" and the name, size and date the file was changed ...

-1
source

In FF there is a dataTransfer method: mozGetDataAt (type, index), when I use type 'text / x-moz-url', it returns the path to the file, for example 'files: /// D: / ABC / file_name. "And funny that the folder / directive has a path with '/' at the end, for example 'files: /// D: / abc / folder1 /'. I wonder if this works in this situation?

-1
source

Source: https://habr.com/ru/post/1436492/


All Articles