Metadata in Cordova's File API

夙愿已清 提交于 2019-12-12 02:08:37

问题


I'm looping through a directory of files in Cordova 3.1.0. For each entry I want the filename and the modification date.

I'm using the getMetadata method on the FileEntry object, which returns the Metadata object in the success callback but I can't see anyway to tie that Metadata object back to the FileEntry object.

This means I have an array of filenames and an array of modification dates but no link between the two.

Here's my code snippet:

// DirectoryEntry.getDirectory callback
function gotPagesDir(d)
{
    var reader = d.createReader();
    reader.readEntries(function(d){
        gotFiles(d);
        appReady();
    }, onError);
}


function gotFiles(entries)
{
    for(var i in entries)
    {
        // __CACHED_FILES is a global scoped object
        __CACHED_FILES[entries[i].name] = {name: entries[i].name};
        entries[i].getMetadata(gotMetadata, metadataError);
    }
}

function gotMetadata(metadata)
{
    var date_modified = metadata.modificationTime;
    // How do I workout which FileEntry object this metadata object belongs to?
}

回答1:


I want to delete all the files or directory which are more than X(15) days old and faced the same challenge.I fixed it in this way. Please have a look. Hope it helps someone

metaDataCallback : function metaDataCallback (dirEntry) {
              return function(metadata) {
                var currentDate = new Date();
                if(assetservice.daysDiff(currentDate, metadata.modificationTime) > Constants.DaysForCachingAssets) {
                  dirEntry.removeRecursively(function(){
                    console.log("File removed");
                  }, function(){
                    console.log("Error while removing file");
                  });
                }
              }
        }




 for (i=0; i<entries.length; i++) {
            if(entries[i].isFile) {
              entries[i].file(function(file) {
                if(daysDiff(currentDate, file.lastModifiedDate) > 15) {
                  entries[i].remove(function(){
                    console.log("File removed");
                  }, function(){
                    console.log("Error while removing file");
                  });
                }
              }, function (error){console.log(error);});
            } else
            {
              //directory
               entries[i].getMetadata(metaDataCallback(entries[i]), function (error){console.log(error);},entries[i]);
            }
}



回答2:


In the end I followed the advice of @dandavis and used entry.file.lastModifiedDate, though even that involved using another callback.

function gotFiles(entries)
{
    for(var i in entries)
    {
       entries[i].file(file_details_callback);
    }
}

function file_details_callback(f)
{
    __CACHED_FILES[f.name] = {name: f.name, lastModifiedDate: f.lastModifiedDate};
}

Hope this helps someone else in the future



来源:https://stackoverflow.com/questions/19688406/metadata-in-cordovas-file-api

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