Skip to content

Instantly share code, notes, and snippets.

@adrianjost
Created July 20, 2019 11:59
Show Gist options
  • Save adrianjost/1bca0be7af6b71078d62cb2da1f1f941 to your computer and use it in GitHub Desktop.
Save adrianjost/1bca0be7af6b71078d62cb2da1f1f941 to your computer and use it in GitHub Desktop.
How to create a filetree from the webbrowser drop event.
const traverseFiles = async (item) => {
if(item.isFile){
return { file: item.file(), path: item.fullPath };
}else if(item.isDirectory){
const entries = await new Promise(resolve => item.createReader().readEntries(resolve));
const files = await Promise.all(entries.map(traverseFiles));
return { path: item.fullPath, files };
}
}
document.getElementById("#drop").addEventListener("drop", (event) => {
const filetree = Promise.all(
Array.from(event.dataTransfer.items).map(item =>
traverseFiles(item.webkitGetAsEntry())
))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment