Ionic 1 Cordova Files (temporary)


Objective

1. To use window.requestFileSystem
2. To use window.TEMPORARY 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


function ($scope, $stateParams,$ionicPlatform) {

$scope.cdv = function (options) {
    console.log('cordova plugins');
   $ionicPlatform.ready(function() { 
    console.log('deviceReady');
    /*cordova task begin*/
    switch (options) {
        case "create-file":
            var type1 = window.TEMPORARY;
            var size1 = 5 * 1024 * 1024;
            window.requestFileSystem(type1, size1, 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":
            var type2 = window.TEMPORARY;
            var size2 = 5 * 1024 * 1024;
            window.requestFileSystem(type2, size2, 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":
            var type3 = window.TEMPORARY;
            var size3 = 5 * 1024 * 1024;
            window.requestFileSystem(type3, size3, 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":

            var type = window.TEMPORARY;
            var size = 5 * 1024 * 1024;
            window.requestFileSystem(type, size, 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*/    

}


NOTE:

List of Error Codes and Meanings

When an error is thrown, one of the following codes will be used.

CodeConstant
1NOT_FOUND_ERR
2SECURITY_ERR
3ABORT_ERR
4NOT_READABLE_ERR
5ENCODING_ERR
6NO_MODIFICATION_ALLOWED_ERR
7INVALID_STATE_ERR
8SYNTAX_ERR
9INVALID_MODIFICATION_ERR
10QUOTA_EXCEEDED_ERR
11TYPE_MISMATCH_ERR
12PATH_EXISTS_ERR

0 Comments: