我正在使用Phonegap的文件api来获取我的应用程序的数据。下载文件没问题,但是我不知道如何更新文件或删除未使用的文件。这是我的代码:
var DATADIR;
var knownfiles = [];
function onFSSuccess(fileSystem) {
fileSystem.root.getDirectory("Android/data/be.emm.mobile",
{create:true},gotDir,onError);
}
function gotDir(d){
console.log("got dir");
DATADIR = d;
var reader = DATADIR.createReader();
reader.readEntries(function(d){
gotFiles(d);
appReady();
},onError);
}
function gotFiles(entries) {
console.log("The dir has "+entries.length+" entries.");
for (var i=0; i<entries.length; i++) {
console.log(entries[i].name+' dir? '+entries[i].isDirectory);
knownfiles.push(entries[i].name);
}
}
function onError(e){
console.log("ERROR");
console.log(JSON.stringify(e));
}
function onDeviceReady() {
$("#status").html("Checking your local cache....");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, null);
}
function appReady(){
$("#status").html("Ready to check remote files...");
$.get("http://mllsdemode.be/EMM-test/download.json", {}, function(res) {
if (res.length > 0) {
$("#status").html("Going to sync some images...");
for (var i = 0; i < res.length; i++) {
if (knownfiles.indexOf(res[i]) == -1) {
console.log("need to download " + res[i]);
var ft = new FileTransfer();
var dlPath = DATADIR.fullPath + "/" + res[i];
console.log("downloading crap to " + dlPath);
ft.download("http://mllsdemode.be/EMM-test/" + escape(res[i]), dlPath, function(e){
console.log("Successful download of "+e.fullPath);
}, onError);
}
}
}
$("#status").html("");
}, "json");
}
function init() {
document.addEventListener("deviceready", onDeviceReady, true);
}
此时只有在地图be.emm.mobile中没有任何内容时才会下载文件。每当应用程序处于活动状态并且有互联网连接时,我想下载文件。我还想删除未使用的文件。在Phonegap文档中,他们给出了这个例子,但我不知道如何实现它。
function success(entry) {
console.log("Removal succeeded");
}
function fail(error) {
alert('Error removing file: ' + error.code);
}
// remove the file
entry.remove(success, fail);
对我有任何提示或建议如何处理此事的人?