删除cacheDirectory缓存科尔多瓦文件系统离子 [英] Delete cacheDirectory cache cordova file system ionic

查看:436
本文介绍了删除cacheDirectory缓存科尔多瓦文件系统离子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个移动应用程序中,我想删除特定用户下载后登录out.Suppose USER1登录缓存图像和下载一些图片和用户​​2登录并下载一些images.User2应该看不到下载的图像任何其他用户。

  downloadFile:功能(downloadLink,downloadFileName,downloadFileMimeType){
                                    $ ionicLoading.show({
                                        模板:'<离子微调>< /离子微调>'
                                    });
                                    VAR的accessToken = $ window.localStorage.getItem(SYSTEM.AUTH_TOKEN);
                                    VAR的选择= {
                                        标题:{
                                            '授权':'承载'+的accessToken
                                        }
                                    };                                    VAR分机;
                                    如果(downloadFileMimeType =='应用程序/ PDF'){
                                        EXT ='.PDF';
                                    }其他{
                                        EXT =名为.jpg;
                                    }
                                    VAR的localPath;
                                    如果(ionic.Platform.isAndroid()){
                                        的localPath = cordova.file.externalCacheDirectory;
                                    }其他{
                                        的localPath = cordova.file.cacheDirectory;
                                    }
                                    。的localPath = + localPath为downloadFileName.trim()取代(/ \\ s + /克,' - ')+分机;
                                    VAR英尺=新的文件传输();
                                    ft.download(downloadLink,localPath来,功能(输入){
                                        $ ionicLoading.hide();
                                        的console.log(在路径下载报告 - + entry.toURL());
                                        cordova.plugins.fileOpener2.open(entry.toURL(),downloadFileMimeType,{
                                            错误:功能(E){
                                                的console.log('错误状态:'+ e.status +' - 错误信息:'+ e.message);
                                            },
                                            成功:函数(FileEntry的){
                                                的console.log(文件打开成功);
                                            }                                        });
                                    },功能失效(错误){
                                        $ ionicLoading.hide();
                                        的console.log(错误边下载,错误code报告 - +错误code);
                                    },真实,期权);
                                }


解决方案

您可以存储在设备中的用户特定文件夹下,通过特定的用户下载的文件,并删除同当其他用户登录,否则,你可以遵循一些文件命名约定特定的用户,而存储在文件夹中的文件并删除特定于特定用户这些文件时,在其他用户登录结果,对于目录删除的样品片段,并使用科尔多瓦文件插件其内容如下:

 函数clearDirectory(){
    如果(ionic.Platform.isAndroid()){
        window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,onFileSystemDirSuccess,失败);
    }其他{
        window.requestFileSystem(LocalFileSystem.PERSISTENT,0,onFileSystemDirSuccess,失败);
    }功能onFileSystemDirSuccess(文件系统){
    变种条目=;
    如果(ionic.Platform.isAndroid()){
        进入=文件系统;
    }其他{
        进入= fileSystem.root;
    }
    entry.getDirectory(DIRECTORY_TO_DELETE,{
            创建:真实,
            独家:假的
        },
        功能(进入){
            entry.removeRecursively(函数(){
                的console.log(删除成功递归);
            },失败);
        },getDirFail);
}功能getDirFail(错误){
    navigator.notification.alert(错误);
};功能失效(错误){
    navigator.notification.alert(错误);
};

}

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

解决方案

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

}

这篇关于删除cacheDirectory缓存科尔多瓦文件系统离子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆