0

I have an app and I want to delete the cached images downloaded by a particular user.Suppose user1 download few images and logout out and then user2 logged in and download few images. User2 should not see cached images or downloaded images of user1.

Cached images showing all the images previously downloaded.

I want cached images to be downloaded after logged out.

   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
  • Please provide us what you have tried so far. Post your code and show us specific problem in your code. Commented Apr 18, 2016 at 4:24

1 Answer 1

1

It's a good question:

Actually if you are using ng-src, then ionic rather the browser itself will cache those images by default, based on the HTML image src attribute. It's a default browser feature.

There are 2 ways you can approach this problem:

1) Don't cache the images:
In order not to cache the images, you will have to change a number appended to the image mostly taken as a version number. To tell the browser the version it is having is an older version.

<img ng-src='api/image/new.png?{{getTimeStamp()}}' ng-click='editImage();'/>

Appending the timestamp here always tricks the browser asking it to fetch the latest image from server.

2) Re-Version the images on logout

Create a $rootScope variable for image version in app.run() method. Like:

app.run(function($rootScope) {
    $rootScope.imgVersion = 0;
})

Now, why as $rootScope because you want the entire app to get updated of this change when the user logs out.

And use this variable as a aparameter in the image src as:

<img ng-src='api/image/new.png?{{imgVersion}}' ng-click='editImage();'/>

So in your case you can change the version number soon the user has logged out.

app.controller('LogoutCtrl', function(){
     //Your log out code here
     $rootScope.imgVersion += 1; //Incrementing the version by 1
});

Working:
In the begining once the app is installed and the app tries to fetch image new.png?0, it is fetched from the server and cached. Cached as new.png?0. Now whenever the app requires new.png?0, it's taken up from cache for display. When the user logs out the value of the route scope variable is incremented and now on-wards it would be fetching new.png?1, which is not there in the cache, so the app will again fetch it afresh, also it will cache it up. So your image itself has been changed in essence.

Hope this helps... :)

References:
[1] https://forum.ionicframework.com/t/using-imagecache-in-ionic/4646/12
[2] https://stackoverflow.com/a/19142498/1904479

1
  • Thanks for your reply.Code attached.
    – Amit Anand
    Commented Apr 18, 2016 at 5:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.