Delete cacheDirectory cache cordova file system ionic

南笙酒味 提交于 2019-12-08 10:17:42

问题


There is a mobile app in which i want to delete the cached images downloaded by a particular user after log out.Suppose user1 logged in and download few images and user2 logged in and downloaded few images.User2 should not see downloaded images of any other user.

      downloadFile : function(downloadLink, downloadFileName, downloadFileMimeType) {
                                    $ionicLoading.show({
                                        template: '<ion-spinner></ion-spinner>'
                                    });
                                    var accessToken = $window.localStorage.getItem(SYSTEM.AUTH_TOKEN);
                                    var options = {
                                        headers : {
                                            'Authorization' : 'Bearer ' + accessToken
                                        }
                                    };

                                    var ext;
                                    if (downloadFileMimeType == 'application/pdf') {
                                        ext = '.pdf';
                                    } else {
                                        ext = '.jpg';
                                    }
                                    var localPath;
                                    if(ionic.Platform.isAndroid()){
                                        localPath = cordova.file.externalCacheDirectory;
                                    }else{
                                        localPath = cordova.file.cacheDirectory;
                                    }
                                    localPath = localPath + downloadFileName.trim().replace(/\s+/g, '-') + ext;


                                    var ft = new FileTransfer();
                                    ft.download(downloadLink, localPath, function(entry) {
                                        $ionicLoading.hide();
                                        console.log("Downloading report on path - " + entry.toURL());
                                        cordova.plugins.fileOpener2.open(entry.toURL(), downloadFileMimeType, {
                                            error : function(e) {
                                                console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
                                            },
                                            success : function(fileEntry) {
                                                console.log('File opened successfully');
                                            }

                                        });
                                    }, function fail(error) {
                                        $ionicLoading.hide();
                                        console.log("Error while downloading report with error code - " + error.code);
                                    }, true, options);
                                }

回答1:


You can store files downloaded by specific user under a user specific folder in device and delete the same when the other user logs in. Else you can follow some file naming convention specific to the users while storing the files in folder and delete those files specific to particular user when the other user logs in.
The sample snippet for directory deletion and its contents using cordova file plugin is as follows:

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");
};

}



来源:https://stackoverflow.com/questions/36733512/delete-cachedirectory-cache-cordova-file-system-ionic

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