Ionic 1 Cordova Files (persistent)


Objective

1. To use window.requestFileSystem
2. To use window.PERSISTENT type of storage
3. To implement file create, read, write and delete.

References


0. Add Cordova

cordova plugin add cordova-plugin-file


1. HTML View

<button ng-click="cdv('create-file')" class="button button-positive button-block">create-file</button>

<button ng-click="cdv('write-file')" class="button button-positive button-block">write-file</button>

<button ng-click="cdv('read-file')" class="button button-positive button-block">read-file</button>

<button ng-click="cdv('remove-file')" class="button button-positive button-block">remove-file</button>

2. JS Controller

// The following is the constructor function for this page's controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams,$ionicPlatform) {

$scope.cdvTasks = function (options) {
    console.log('cdvTasks begin');
   $ionicPlatform.ready(function() {
    console.log('deviceReady');
    /*cordova task begin*/
    switch (options) {
        case "create-file":
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successCallback1, errorCallback1)

            function successCallback1(fs) {
                fs.root.getFile('log.txt', {
                    create: true,
                    exclusive: true
                }, function(fileEntry) {
                    alert('File creation successfull!')
                }, errorCallback1);
            }

            function errorCallback1(error) {
                alert("ERROR: " + error.code)
            }
            break;
        case "write-file":
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successCallback2, errorCallback2)

            function successCallback2(fs) {
                fs.root.getFile('log.txt', {
                    create: true
                }, function(fileEntry) {

                    fileEntry.createWriter(function(fileWriter) {
                        fileWriter.onwriteend = function(e) {
                            alert('Write completed.');
                        };

                        fileWriter.onerror = function(e) {
                            alert('Write failed: ' + e.toString());
                        };

                        var blob = new Blob(['Lorem Ipsum'], {
                            type: 'text/plain'
                        });
                        fileWriter.write(blob);
                    }, errorCallback2);
                }, errorCallback2);
            }

            function errorCallback2(error) {
                alert("ERROR: " + error.code)
            }

            break;
        case "read-file":
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successCallback3, errorCallback3)

            function successCallback3(fs) {
                fs.root.getFile('log.txt', {}, function(fileEntry) {

                    fileEntry.file(function(file) {
                        var reader = new FileReader();

                        reader.onloadend = function(e) {
                         
                            alert(this.result);
                        };
                        reader.readAsText(file);
                    }, errorCallback3);
                }, errorCallback3);
            }

            function errorCallback3(error) {
                alert("ERROR: " + error.code)
            }

            break;
        case "remove-file":
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, successCallback4, errorCallback4)

            function successCallback4(fs) {
                fs.root.getFile('log.txt', {
                    create: false
                }, function(fileEntry) {

                    fileEntry.remove(function() {
                        alert('File removed.');
                    }, errorCallback4);
                }, errorCallback4);
            }

            function errorCallback4(error) {
                alert("ERROR: " + error.code)
            }


            break;
    } /*switch*/
    function cdvFail(error) {
        alert("ERROR: " + error.code)
    }       
        /*cordova task end*/
});/*device ready*/
}/*app task*/ 

}

0 Comments: