Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript file dropping and reading directories - Asynchronous Recursion
    primarykey
    data
    text
    <p>So I'm trying to create a file dropper web application. Right now, a user can drop files on the screen and I can read them, including all the files in a directory that was dropped. But, I don't know when the script is done reading the files.</p> <p>Some code:</p> <p>This first function handles a 'drop' event and will loop through each file and send it to another function that will read its contents.</p> <pre><code>function readDrop( evt ) { for( var i = 0; i &lt; evt.dataTransfer.files.length; i++) { var entry = evt.dataTransfer.items[i].webkitGetAsEntry(); if(entry) readContents(entry, ""); } //Do stuff after all files and directories have been read. } </code></pre> <p>This function is a recursive FileEntry reader. If it is a file, I will read the FileEntry. If it is a directory, it will loop through the contents and pass it through this function.</p> <pre><code>function readContents(entry, path) { if( entry.isFile ) { readFileData( entry, path, function(fileData) { _MyFiles.push( fileData ); }); } else if( entry.isDirectory ) { var directoryReader = entry.createReader(); var path = path + entry.name; directoryReader.readEntries(function(results) { for( var j = 0; j &lt; results.length; j++ ) { readContents(entry, path); } }, errorHandler) } } </code></pre> <p>And here is my function for reading the files. The callback just pushes the fileData object to a global array</p> <pre><code>function readFileData(entry, path, callback) { var fileData = {"name": entry.name, "size": 0, "path": path, "file": entry}; entry.file(function(file) { fileData["size"] = file.size; callback( fileData ); } } </code></pre> <p>I'm not sure where to go from here so that I can have a callback when all files and directories have been read.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload