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

核能气质少年 提交于 2019-12-17 19:46:10

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/10234515/html5-file-system-how-to-read-directories-using-directory-reader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!