FileSystem problem - cordova Ionic Application

Hello everyone,

I have a problem with requestFileSystem function. I have the following error in firefox:

Error: requestFileSystem is not defined
$scope.save@http://localhost:8100/js/controllers/AddCtrl.js:14:9
$scope.addAss@http://localhost:8100/js/controllers/AddCtrl.js:9:9
anonymous/fn@http://localhost:8100/lib/ionic/js/ionic.bundle.js line 26457 > Function:2:206
ngEventHandler/</callback@http://localhost:8100/lib/ionic/js/ionic.bundle.js:36610:17
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:8100/lib/ionic/js/ionic.bundle.js:29158:16
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:8100/lib/ionic/js/ionic.bundle.js:29257:18
ngEventHandler/<@http://localhost:8100/lib/ionic/js/ionic.bundle.js:36615:17
createEventHandler/eventHandler@http://localhost:8100/lib/ionic/js/ionic.bundle.js:16583:9
triggerMouseEvent@http://localhost:8100/lib/ionic/js/ionic.bundle.js:2948:3
tapClick@http://localhost:8100/lib/ionic/js/ionic.bundle.js:2937:3
tapMouseUp@http://localhost:8100/lib/ionic/js/ionic.bundle.js:3013:5
EventListener.handleEvent*tapEventListener@http://localhost:8100/lib/ionic/js/ionic.bundle.js:2921:5
ionic.tap.register@http://localhost:8100/lib/ionic/js/ionic.bundle.js:2734:5
FrameRequestCallback*ionic.DomUtil.requestAnimationFrame@http://localhost:8100/lib/ionic/js/ionic.bundle.js:185:14
domReady@http://localhost:8100/lib/ionic/js/ionic.bundle.js:141:7
EventListener.handleEvent*@http://localhost:8100/lib/ionic/js/ionic.bundle.js:147:5
@http://localhost:8100/lib/ionic/js/ionic.bundle.js:133:1
@http://localhost:8100/lib/ionic/js/ionic.bundle.js:22:2

:
thanks for your help
problem after alert "OK 1"
Here is my code

app.controller('AddCtrl', function ($scope, $location) {
 
    $scope.moment = {
        image: 'images/place.PNG',
        nom: ''
    };
 
    $scope.moments = [];
 
    $scope.addAss = function () {
        $scope.moments.push($scope.moment);
        $scope.save();
    };
 
    $scope.save = function () {
        alert ('Ok 1');
        requestFileSystem(window.PERSISTENT, 0, fsReceived, errorHandler);
        $location.path('/');
 
    };
 
    var fsReceived = function (fileSystem) {
        alert ('Ok 2');
        fileSystem.root.getFile('moments.json', { create: true, exclusive: false }, feReceived, errorHandler);
    };
 
    var feReceived = function (fileEntry) {
        fileEntry.createWriter(fwReceived, errorHandler);
    };
 
    var fwReceived = function (fileWriter) {
        var momentsText = angular.toJson($scope.moments);
        fileWriter.write(momentsText);
    };
 
    var errorHandler = function (error) {
 
    };
 
})

hi ,
i didn’t use this service but
i think you need the device to be ready

ie : ionic.Platform.ready(function(){})

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

same problem :

Error: LocalFileSystem is not defined

code

app.controller('AddCtrl', function ($scope, $location, $ionicPlatform) {
  $ionicPlatform.ready(function() {

      $scope.addAss = function () {          
            $scope.save();
      };
      
    $scope.save = function () {
            alert ('ok 1');
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsReceived, errorHandler);
            $location.path('/');
    };

    var fsReceived = function (fileSystem) { 
            alert ('ok 2');    
    };

    var errorHandler = function (error) {
    };

  });
});

please read this maybe this will help you

http://www.html5rocks.com/en/tutorials/file/filesystem/

I’ve already looked at this url. But no solution.

can you give more details about your goals

do you do the test in browser (and which one) or mobile phone or simulator
and why you don’t use ngCordova for file api

http://ngcordova.com/docs/plugins/file/

I test with firefox or chrome.
I do:> Ionic serve

I just tested:

 Error: cordova is not defined

code :

app.controller('AddCtrl', function($scope, $cordovaFile, $location, $ionicPlatform) {

      $ionicPlatform.ready(function() {
        $scope.addAss = function () { 
          
      $cordovaFile.createFile(cordova.file.dataDirectory, "new_file.txt", true)
        .then(function (success) {
          alert('ok1');
        }, function (error) {
          alert('ok2');
        });

      };

      });
});

hi sorry i had to do some things :smile:

so you do the test in browser with ionic serve but cordova is not defined
this is ok in simulator or device is OK

but i you want tests this in the browser
you need to add:

ionic platform add browser

ionic run browser

Thank you ! it works !

But I have another error : Uncaught TypeError: fileEntry.creatWriter is not a function

Here is my code :

app.controller('AddCtrl',function($scope,$location) {
    
    $scope.dataAss = {
      nom:    ''
    };

    $scope.AssTab = [];

    $scope.addAss = function(){
      $scope.AssTab.push($scope.dataAss);
      $scope.save();
    };

    $scope.save = function(){

        if (window.webkitRequestFileSystem) {
                     window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024,fsReceived,errorHandler);
        } 
        else {
                    window.requestFileSystem(window.PERSISTENT, 10*1024*1024, fsReceived, errorHandler);
        }

      //requestFileSystem(window.PERSISTENT, 0, fsReceived, errorHandler);
      $location.path('templates/assurances.html')
    };

    var fsReceived = function(fileSystem){
      fileSystem.root.getFile('assurance.json',{create : true, exclusive : false }, feReceived, errorHandler)
    };

    var feReceived = function(fileEntry){
      fileEntry.creatWriter(fwReceived,errorHandler);
    };

    var fwReceived =function(fileWriter){
      var AssText = angular.toJson($scope.AssTab);
      fileWriter.write(AssText);
    };

    var errorHandler = function(error){
      alert("error : "+error);
    };

});

hi i didn’t test your code
but you have a typo error

var feReceived = function(fileEntry){
fileEntry.creatWriter(fwReceived,errorHandler);
};

this should be fileEntry.createWriter

Thanks !

I have an error: Uncaught TypeMismatchError: The type of an object was incompatible with the expected type of the parameter associated to the object. AddCtrl.js:76

75 Alert(AssText);
76 fileWriter.write(AssText);

Alert (AssText) = [{“nom”:“test”},{“nom”:“test”}]

I found the solution

    var fwReceived =function(fileWriter){
      var AssText = angular.toJson($scope.AssTab);
      var blob = new Blob([AssText], {type: "text/plain"});
      fileWriter.write(blob);
    };

Thanks a lot for your help !
Good day

1 Like

@ItamarCohen.
I’ve had similar problem using ionic/file-transfer download. In the catch function I get an error: ‘PATH_EXISTS_ERR’, but i’m downloading a new file. This happens intermitantly.
Nice name, by the way :slight_smile: