使用cordova插件删除iOS中应用程序的临时目录

delete temp directory of an app in iOS using cordova plugin

本文关键字:应用程序 cordova 插件 删除 iOS 使用      更新时间:2023-09-26

我使用ionic框架在iOS的Library目录中的应用程序中存储一些图像。是否可以使用Cordova插件文件和Cordova插入文件传输删除相同的目录?

有人能帮忙吗?

我下载图像的代码是:

downloadImage: function(url, fileName) {
        var deferred = $q.defer();
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
                fs.root.getDirectory(
                    LOCAL_STORAGE_KEYS.app, {
                        create: true
                    },
                    function(dirEntry) {
                        // console.log(arguments);
                        dirEntry.getFile(
                            fileName, {
                                create: true,
                                exclusive: false
                            },
                            function(fe) {
                                console.log(arguments);
                                var p = fe.toURL();
                                console.log("In service the url path:", p);
                                fe.remove();
                                var ft = new FileTransfer();
                                console.log('File Transfer instance:', ft);
                                ft.download(
                                    encodeURI(url),
                                    p,
                                    function(entry) {
                                        console.log('In service the entry callback:', entry);
                                        if (entry && entry.toURL) {
                                            deferred.resolve(entry.toURL());
                                        } else {
                                            console.log('No entry:', arguments);
                                            deferred.resolve();
                                        }
                                    },
                                    function(err) {
                                        console.log('Getting rejected:', err);
                                        deferred.reject(err);
                                    },
                                    false,
                                    null
                                );
                            },
                            function() {
                                deferred.reject(new Error('get file  failed'));
                            }
                        );
                    }
                );
            },
            function() {
                deferred.reject(new Error('get directory failed'));
            });
        return deferred.promise;
    }    

以下是使用cordova文件插件删除目录及其内容的示例代码片段:

function clearDirectory() {
    if (ionic.Platform.isAndroid()) {
        window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemDirSuccess, fail);
    } else {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemDirSuccess, fail);
    }
    function onFileSystemDirSuccess(fileSystem) {
        var entry = "";
        if (ionic.Platform.isAndroid()) {
            entry = fileSystem;
        } else {
            entry = fileSystem.root;
        }
        entry.getDirectory("DIRECTORY_TO_DELETE", {
                create: true,
                exclusive: false
            },
            function(entry) {
                entry.removeRecursively(function() {
                    console.log("Remove Recursively Succeeded");
                }, fail);
            }, getDirFail);
    }
    function getDirFail(error) {
        navigator.notification.alert("Error");
    };
    function fail(error) {
        navigator.notification.alert("Error");
    };
}