2014-08-27 89 views
1

我越来越迷惑如何递归读取文件列表。 假设我有我的filesytem API的根目录3个文本文件递归读取目录中的所有文件 - FileSystem API

  • text1.txt
  • text2.txt
  • text3.txt

我的目标是阅读每一个文本文件通过一个然后将每个文件中的所有条目连接成一个字符串,但是我现在处于丢失状态 如何在Javascript FileSystem API中执行此操作。

window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler); 
function onInitFs(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 (!results.length) { 
     readAllFiles(results); 
     } else { 
     entries = entries.concat(toArray(results)); 
     readEntries(); 
     } 
    }, errorHandler); 
    }; 
    readEntries(); // Start reading dirs. 
} 

function readAllFiles(entries){ 
    //Loop thru all the files and read each entries 
} 

我已经看过如何读取一个文本文件,但我不知道如何实现读取所有文件并连接值。 他们都实现回调函数,所以我对如何处理它感到困惑。请点任何一点?

我其实一直在立足我所有的作品在这个http://www.html5rocks.com/en/tutorials/file/filesystem

更新2 按@Johan 我居然改变了我的代码,以使用回拨

window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler); 
    function onInitFs(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 (!results.length) { 
      readAllFiles(results, concatMessages); 
      } else { 
      entries = entries.concat(toArray(results)); 
      readEntries(); 
      } 
     }, errorHandler); 
     }; 
     readEntries(); // Start reading dirs. 
    } 


    var concatMessage = ''; 

    function concatMessages(message){ 
     concatMessage += message; 
    } 

    function readAllFiles(logs, callBack) { 
     logs.forEach(function(entry, iCtr) { 
      var message; 

      entry.file(function(file) { 
       var reader = new FileReader(); 
       reader.onloadend = function(e) { 
        //message += this.result; 
        if(callBack) 
         callBack('==================' + iCtr + '=========================='); 
         callBack(this.result); 
       }; 
       reader.readAsText(file); // Read the file as plaintext. 
      }, function(err) { 
       console.log(err); 
      }); 

     }); 

    } 

我唯一的问题是这个回调函数不是顺序的。 它先读取text2.txt,然后text3.txt然后text1.txt,所以最终结果不是顺序的,这不是我想要做的。还有什么提示?

+0

嗯,是不是你的代码在所有路径完全递归?什么会让你的函数退出? – Johan 2014-08-27 13:15:35

+0

@Johan Ooopss..sorry ..修复了我的代码。在您访问这些文件后,我想逐个读取每个文件,并对条目进行协调。在readAllFiles中,我对如何读取每个文件感到困惑,因为FileSystem API由很多回调组成,我遇到了如何连接结果的问题。 – 2014-08-27 13:24:44

+0

我会利用承诺。不知道有多少浏览器支持它,但你总是可以包含jQuery – Johan 2014-08-27 14:12:36

回答

0

强烈建议您考虑使用类似caolan的async库来实现此目的。

你可以做这样的事情:

async.each(openFiles, function(file, callback) { 

    // Perform operation on file here. 
    console.log('Processing file ' + file); 
    callback(); 
}, function(err) { 
    // if any of the file processing produced an error, err would equal that error 
    if (err) { 
     // One of the iterations produced an error. 
     // All processing will now stop. 
     console.log('A file failed to process'); 
    } else { 
     // do your concatenation here 
    } 
}); 
+0

谢谢。我很高兴你明白我在做什么。但不幸的是,目前我无法使用第三方库。这是一个异步处理的情况,但是我的JavaScript知识并没有那么遥远。 – 2014-08-27 14:17:06

+0

你可以谷歌周围找到一些类似的例子,如何使用'forEach'并写一个承诺回调。在这种情况下,不需要使用第三方库。 – brandonscript 2014-08-27 15:19:33

相关问题