9

I am not able to get the cordova file system to work. I have a project with the following dependencies:

com.ionic.keyboard 1.0.3 "Keyboard"
org.apache.cordova.console 0.2.12 "Console"
org.apache.cordova.device 0.2.13 "Device"
org.apache.cordova.file 1.3.2 "File"
org.apache.cordova.file-transfer 0.4.8 "File Transfer"

In app.js I define a dependency on a controller module:

angular.module('starter', ['ionic', 'controllers']);

The controller code is basically this:

angular.module('controllers', [])
  .controller('GalleryCtrl', function ($scope) {
    function success() {...}
    function error() {...}
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
  }
}

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.

EDIT: This is the according html markup:

<body ng-app="starter" ng-controller="GalleryCtrl">
<ion-nav-view>
    <ion-slide-box id="slideBox">
        <ion-slide ng-repeat="..."> <!-- details omitted -->
        </ion-slide>
    </ion-slide-box>
</ion-nav-view>
</body>
Bastian
  • 4,638
  • 6
  • 36
  • 55

3 Answers3

11

You simply aren't waiting for deviceReady event to be fired and thus the File plugin isn't loaded yet. Change

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);

to

document.addEventListener("deviceready", function() { 
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
}, false);

The LocalFileSystem.PERSISTENT might be undefined even after that (has been for me while emulating etc.) but it can be replaced with 1 as it is just a constant.

Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
  • I should add that apparently the file-transfer plugin prevented the `deviceready` event from being triggered. After removing this plugin and registering above event listener every worked fine. – Bastian Jan 20 '15 at 08:10
  • 2
    Note that because of the title of this question people like me may come here that they simply didn't add cordova file plugin, the solution: `cordova plugin add cordova-plugin-file` – Ehsan88 Dec 09 '17 at 08:46
-1

One of the reasons for the requestFileSystem not being available is because the device is not ready.

Try running the code once the window is ready:

$scope.$on('$ionicView.enter', function(event, data) {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
});

I get the LocalFileSystem is not defined in my editor because its a plugin (from my understanding) but once the window is loaded I can use the requestFileSystem and LocalFileSystem works as expected.

Phil Hannent
  • 12,047
  • 17
  • 71
  • 118
  • While your approach suppresses the error messages it does so by just not firing the `$ionicView.enter` event. Hence, `window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);` is never executed. Any idea why this might happen? – Bastian Jan 16 '15 at 16:13
-1

For ionic inject $ionicPlatform and then use :

$ionicPlatform.ready(function() {       
   window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
}, false);
avck
  • 3,535
  • 3
  • 26
  • 38