LMSouq
moodle-core Open

Detecting folders/directories in javascript FileList objects

CO
Community Contributor
1 month ago
3 views
Problem Description
I have recently contributed some code to Moodle which uses some of the capabilities of HTML5 to allow files to be uploaded in forms via drag and drop from the desktop (the core part of the code is here: https://github.com/moodle/moodle/blob/master/lib/form/dndupload.js for reference). This is working well, except for when a user **drags** a **folder / directory** instead of a real file. Garbage is then uploaded to the server, but with the filename matching the folder. What I am looking for is an easy and reliable way to **detect** the presence of a **folder** in the **FileList** object, so I can skip it (and probably return a friendly error message as well). I've looked through the documentation on MDN, as well as a more general web search, but not turned up anything. I've also looked through the data in the Chrome developer tools and it appears that the **'type'** of the File object is consistently set to **""** for folders. However, I'm not quite convinced this is the most reliable, cross-browser detection method. Does anyone have any better suggestions?

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base