如何删除使用 phonegap-plugin-contentsync 下载的缓存文件?
How to delete cached files downloaded with phonegap-plugin-contentsync?
在我们使用 cordova 开发的应用程序中,我们需要删除借助 contentsync plugin 下载的文件。
这是我使用 file plugin 实现的。
function clearCache() {
localStorage.clear();
// remove the stuff from the file system as well
deleteAllFilesInPath(cordova.file.dataDirectory);
deleteAllFilesInPath(cordova.file.cacheDirectory);
deleteAllFilesInPath(cordova.file.externalDataDirectory);
deleteAllFilesInPath(cordova.file.externalCacheDirectory);
}
function deleteAllFilesInPath(path) {
if (!path) { return; }
window.resolveLocalFileSystemURL(path, function (entry) {
if (entry.isDirectory) {
var dirReader = entry.createReader();
dirReader.readEntries(function(entries) {
console.log(entries);
for (var i in entries) {
deleteFileOrDirEntry(entries[i]);
}
})
}
})
}
function deleteLocalPath(path) {
window.resolveLocalFileSystemURL(path,
deleteFileOrDirEntry,
function (error) {
log.error("failed to access ", path, " error: ", JSON.stringify(error));
});
}
function deleteFileOrDirEntry(entry) {
if (entry.isDirectory) {
entry.removeRecursively(function (code) {
log.info("deleted dir ", entry.fullPath, " ", code);
},
function (error) {
log.error("failed to remove dir ", entry.fullPath, " error: ", JSON.stringify(error))
});
} else {
entry.remove(function (code) {
log.info("deleted file ", entry.fullPath, " ", code);
},
function (error) {
log.error("failed to remove file ", entry.fullPath, " error: ", JSON.stringify(error))
});
}
}
这在 Android 上运行良好。在 iOS,我遇到了一个问题,当我再次同步内容时,它失败并出现错误:
Task ... completed with error: The operation couldn't be completed. No such file or directory
Error downloading type: 2, responseCode: 200
但是当我退出应用程序并重新启动时,它可以正常工作。
我发现没有API可以直接在同步插件中删除文件。
我是否需要重置插件/通知它有关已删除文件的信息?
啊,似乎在 iOS 上,如果缓存目录也被清除,插件会感到困惑。如果我这样做:
function clearCache() {
localStorage.clear();
// remove the stuff from the file system as well
deleteAllFilesInPath(cordova.file.dataDirectory);
// deleteAllFilesInPath(cordova.file.cacheDirectory);
deleteAllFilesInPath(cordova.file.externalDataDirectory);
// deleteAllFilesInPath(cordova.file.externalCacheDirectory);
}
有效。
(问题仍然是这种删除文件的方式是否适用于同步插件,或者是否有/应该有一个 API。)
在我们使用 cordova 开发的应用程序中,我们需要删除借助 contentsync plugin 下载的文件。
这是我使用 file plugin 实现的。
function clearCache() {
localStorage.clear();
// remove the stuff from the file system as well
deleteAllFilesInPath(cordova.file.dataDirectory);
deleteAllFilesInPath(cordova.file.cacheDirectory);
deleteAllFilesInPath(cordova.file.externalDataDirectory);
deleteAllFilesInPath(cordova.file.externalCacheDirectory);
}
function deleteAllFilesInPath(path) {
if (!path) { return; }
window.resolveLocalFileSystemURL(path, function (entry) {
if (entry.isDirectory) {
var dirReader = entry.createReader();
dirReader.readEntries(function(entries) {
console.log(entries);
for (var i in entries) {
deleteFileOrDirEntry(entries[i]);
}
})
}
})
}
function deleteLocalPath(path) {
window.resolveLocalFileSystemURL(path,
deleteFileOrDirEntry,
function (error) {
log.error("failed to access ", path, " error: ", JSON.stringify(error));
});
}
function deleteFileOrDirEntry(entry) {
if (entry.isDirectory) {
entry.removeRecursively(function (code) {
log.info("deleted dir ", entry.fullPath, " ", code);
},
function (error) {
log.error("failed to remove dir ", entry.fullPath, " error: ", JSON.stringify(error))
});
} else {
entry.remove(function (code) {
log.info("deleted file ", entry.fullPath, " ", code);
},
function (error) {
log.error("failed to remove file ", entry.fullPath, " error: ", JSON.stringify(error))
});
}
}
这在 Android 上运行良好。在 iOS,我遇到了一个问题,当我再次同步内容时,它失败并出现错误:
Task ... completed with error: The operation couldn't be completed. No such file or directory
Error downloading type: 2, responseCode: 200
但是当我退出应用程序并重新启动时,它可以正常工作。
我发现没有API可以直接在同步插件中删除文件。 我是否需要重置插件/通知它有关已删除文件的信息?
啊,似乎在 iOS 上,如果缓存目录也被清除,插件会感到困惑。如果我这样做:
function clearCache() {
localStorage.clear();
// remove the stuff from the file system as well
deleteAllFilesInPath(cordova.file.dataDirectory);
// deleteAllFilesInPath(cordova.file.cacheDirectory);
deleteAllFilesInPath(cordova.file.externalDataDirectory);
// deleteAllFilesInPath(cordova.file.externalCacheDirectory);
}
有效。
(问题仍然是这种删除文件的方式是否适用于同步插件,或者是否有/应该有一个 API。)