在phonegap中检查目录中的文件

时间:2012-07-19 09:48:55

标签: javascript cordova

这篇文章中有2个问题,对于经验丰富的js家伙来说,它们可能很简单: - )

首先;为什么当我传递它时,readEntries中的“filename”未定义?

二;当目录为空时,为什么总是如此?

继承我的代码:我用“women.png”这样的字符串调用getPicturepath

function getPicturePath(filename){
    alert(filename); //is correct
    var reader = DATADIR.createReader();
    reader.readEntries(function(entries, filename){
    alert(filename);//is undefined ???
        var doWeHaveIt = function(entries,filename){
            checkForFile(entries,filename)
            };
        if(doWeHaveIt){
            alert('allready have: '+DATADIR.fullPath+filename);

        } else {
            alert('need to download file: '+filename);
        }
    },onError);
}

function checkForFile(entries,filename){
    console.log("The dir has "+entries.length+" entries.");
    if(entries.indexOf(filename)!=-1){
        alert(filename+' allready exists');
        return true;
    } else {
        alert(filename+" doesn't exists");
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

reader.readEntries(function(entries, filename){

这是定义参数 entriesfilename的函数。

例如,此函数可能会执行以下操作:

readEntries: function( callback ) {
    // do something, then
    callback( some, datas );
}

如果您只想在此功能中使用filename,请使用它。像这样:

function getPicturePath(filename){
    alert(filename); //is correct
    var reader = DATADIR.createReader();
    reader.readEntries(function(entries){
        alert(filename);// is still correct

第二部分(总是如此)是因为:

function hi() {}

if ( hi ) {
    // You're always getting there.
}

我写的是完全你做了什么。我让你猜怎么纠正: - )