【问题标题】:Recursive reading on all files in a directory - FileSystem API递归读取目录中的所有文件 - FileSystem API
【发布时间】:2014-08-27 13:04:11
【问题描述】:

我对如何递归读取文件列表感到困惑。 假设我的文件系统 api 根目录中有 3 个文本文件

  • 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 所以最终结果不是连续的,这不是我想要做的。还有更多提示吗?

【问题讨论】:

  • 嗯,您的代码不是在所有路径上都完全递归吗?什么会让你的函数退出?
  • @Johan Ooopss..sorry.. 修复了我的代码。在您可以访问文件后,我想一个一个地读取每个文件并连接条目。在 readAllFiles 中,我对如何读取每个文件感到困惑,因为 FileSystem API 由大量回调组成,并且我遇到了如何连接结果的问题。
  • 我会使用承诺。不确定有多少浏览器支持它,但你总是可以包含 jQuery
  • 如果您不想使用库,请使用带有 2 个参数的回调:文件号和文本。等待所有完成并连接字符串
  • @Johan,我实际上按照您的建议尝试了回调。你能检查我的更新 2 吗?谢谢

标签: javascript html html5-filesystem


【解决方案1】:

强烈建议您考虑使用类似 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
    }
});

【讨论】:

  • 谢谢。我很高兴你明白我想要做什么。但不幸的是,我目前无法使用 3rd 方库。这是一个异步处理的例子,但我的 javascript 知识并没有那么远。
  • 你可以google一下,找到一些类似的例子来说明如何使用forEach并编写一个promise回调。在这种情况下无需使用 3rd 方库。
猜你喜欢
  • 2018-07-02
  • 2020-05-27
  • 1970-01-01
  • 2018-03-05
  • 2017-05-18
  • 1970-01-01
  • 2011-04-26
  • 2011-09-27
  • 1970-01-01
相关资源
最近更新 更多