0

我需要读取目录中的许多文件。
我可以获取目录的文件名,但不读取文件内容

下面是我的代码

$("#import-offline").click( function(){
            var localFolder = "ico/recebe/importa/";
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
                  fs.root.getDirectory(localFolder, {}, function(dirEntry){
                  var dirReader = dirEntry.createReader();
                  dirReader.readEntries(function(entries) {
                        for(var i = 0; i < entries.length; i++) {
                            var entry = entries[i];
                            if (entry.isFile){  // here works
                                var arquivoPath = entry.fullPath;
                                var reader = new FileReader();
                                reader.onloadend = function(evt) {
                                    $("#msgSync").html(evt.target.result);
                                };
                                reader.readAsText(arquivoPath);

                            }
                        }
                  }, fail);
                }, fail);
            });

        });    

    });
4

1 回答 1

0

解决

$("#importOffline").click(function(){
    $('#msgSync').html("Iniciando importação aguarde...");
    var localFolder = "ico/recebe/importa";
    console.log("Chamou a função");
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
          console.log("Abriu File System");  
          // rodar log no console
          // adb logcat CordovaLog:V *:S 
          fs.root.getDirectory(localFolder, {create:false}, function(dirEntry){
          console.log(">>>>> Abriu a pasta");
          var dirReader = dirEntry.createReader();
          dirReader.readEntries(function(entries) {
                console.log("Lendo diretórios");
                for(var i = 0; i < entries.length; i++) {
                    console.log("Arquivos "  + i);
                    var entry = entries[i];
                    if (entry.isFile){
                        var arquivoPath = entry.fullPath;
                        var n = entry.name.split(".");
                        tabela = n[0];
                        console.log("Lendo " + arquivoPath);
                        var reader = new FileReader();

                        reader.onloadend = function(evt) {
                            $('#msgSync').html("");
                            $('#msgSync').html("Importando "+tabela+", aguarde..." + i);
                            var arq = evt.target.result.split( /\r?\n/g );
                            console.log("=====================================");
                            importaTxt(tabela, arq);

                        };
                        reader.readAsText(arquivoPath);
                    }
                }

                $('#msgSync').html("");
                navigator.notification.alert("Importação finalizada", null, "mSAC");
          }, fail);
        }, fail);
    });
于 2012-07-17T14:09:02.047 回答