Search code examples
cordovaionic-frameworkcordova-plugins

Ionic : LocalFileSystem is not defined in Cordova


I am not able to get the cordova file system to work. I have a project with the following dependencies: In app.js I define a dependency on a controller module:

exampleApp.controller("FileController", function($scope, $ionicLoading) {

$scope.download = function() {
    $ionicLoading.show({
      template: 'Loading...'
    });
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
        fs.root.getDirectory(
            "ExampleProject",
            {
                create: true
            },
            function(dirEntry) {
                dirEntry.getFile(
                    "test.png", 
                    {
                        create: true, 
                        exclusive: false
                    }, 
                    function gotFileEntry(fe) {
                        var p = fe.toURL();
                        fe.remove();
                        ft = new FileTransfer();
                        ft.download(
                            encodeURI("http://ionicframework.com/img/ionic-logo-blog.png"),
                            p,
                            function(entry) {
                                $ionicLoading.hide();
                                $scope.imgFile = entry.toURL();
                            },
                            function(error) {
                                $ionicLoading.hide();
                                alert("Download Error Source -> " + error.source);
                            },
                            false,
                            null
                        );
                    }, 
                    function() {
                        $ionicLoading.hide();
                        console.log("Get file failed");
                    }
                );
            }
        );
    },
    function() {
        $ionicLoading.hide();
        console.log("Request for filesystem failed");
    });
}

});

What I then get is:

LocalFileSystem is not defined

Also, requestFileSystem is undefined. What could be the reason for this behavior?

I am using cordova 4.1.2 and ionic 1.3.1.


Solution

  • You need to install cordova file plugin to use these functionalities, add plugin to your project by this command: ionic plugin add cordova-plugin-file

    Note: Cordova file system is supposed to work with real devices or emulators, not browsers.