如何删除通过phonegap-plugin-contentsync下载的缓存文件?

时间:2018-11-15 12:18:48

标签: cordova phonegap-plugins

在我们使用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。 我是否需要重置插件/通知已删除文件?

1 个答案:

答案 0 :(得分:0)

嗯,似乎在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。)