Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 File System - How to read directories using directory reader?

I loaded a directory using file input and webkitdirectory as mentioned below.

<input  id="file_input"  type="file" webkitdirectory directory  />

After directory selected I can read file size and other information. My question is how to read this directory using DirectoryReader interface.

I tried with below code, but not success. results.length becomes zero. Am I missing anything?

window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, function(fs) {
        var dirReader = fs.root.createReader();
        var entries = [];
        // Call the reader.readEntries() until no more results are returned.
        var readEntries = function() {
            dirReader.readEntries(function(results) {
                // If no more results are returned, we're done.
                if (!results.length) {
                    // Sort list by name of entry.
                    entries.sort(function(a, b) {
                        return a.name < b.name ? -1 :
                        b.name < a.name ? 1 : 0;
                    });
                   // listResults(entries); // Render the list.
                } else {
                    // Add in these results to the current list.
                    entries = entries.concat(toArray(results));
                    readEntries();
                }
            }, errorHandler);
        };
        readEntries(); // Start reading the directory.
    }, errorHandler);

Any help is appreciated.

like image 604
Joe Avatar asked Apr 19 '12 18:04

Joe


1 Answers

To read the content of a directory :

fs.root.getDirectory('Documents', {}, function(dirEntry){
  var dirReader = dirEntry.createReader();
  dirReader.readEntries(function(entries) {
    for(var i = 0; i < entries.length; i++) {
      var entry = entries[i];
      if (entry.isDirectory){
        console.log('Directory: ' + entry.fullPath);
      }
      else if (entry.isFile){
        console.log('File: ' + entry.fullPath);
      }
    }

  }, errorHandler);
}, errorHandler);

In the code above, the isDirectory and isFile properties are used in order to obtain a different output for directories and files, respectively. Additionally, we use the fullPath property in order to get the full path of the entry, instead of its name only.

From : http://net.tutsplus.com/tutorials/html-css-techniques/toying-with-the-html5-filesystem-api/

like image 70
fdicarlo Avatar answered Sep 26 '22 20:09

fdicarlo