Kurlrip
6/17/2016 - 4:15 PM

Fonction outil à utiliser sur cordova avec le plugin cordova-plugin-ftp

Fonction outil à utiliser sur cordova avec le plugin cordova-plugin-ftp

.controller('FtpCtrl', function($scope, $cordovaFile) {

      $scope.ftpTest = function(){
        if (window.cordova.plugin.ftp) {
          alert("xtest: ftp: found");
          // 1. connect to one ftp server, then you can do any actions/cmds
          window.cordova.plugin.ftp.connect("91.121.254.36", "AP_K.gimer", "**********", function() {
            alert("xtest: ftp: connect ok");
            // 2. list one dir, note that just can be dir, not file
            window.cordova.plugin.ftp.ls("/Android", function(fileList) {
              alert("xtest: ftp: list ok");
              if (fileList && fileList.length > 0) {
                alert("xtest: ftp: The last file'name is " + fileList[fileList.length - 1].name);
                alert("xtest: ftp: The last file'type is " + fileList[fileList.length - 1].type);
                alert("xtest: ftp: The last file'link is " + fileList[fileList.length - 1].link);
                alert("xtest: ftp: The last file'size is " + fileList[fileList.length - 1].size);
                alert("xtest: ftp: The last file'modifiedDate is " + fileList[fileList.length - 1].modifiedDate);
                // 3. create one dir on ftp server
                window.cordova.plugin.ftp.mkdir("/Android/mkdir", function(ok) {
                  alert("xtest: ftp: mkdir ok=" + ok);
                  // 4. upload local file to remote, you can rename at the same time. arg1: local file, arg2: remote file.
                  // make sure you can access and read the local file.

                  //window.cordova.plugin.ftp.upload(cordova.file.externalRootDirectory + "test.txt","/Android/mkdir/test.txt",  function(percent) {
                  window.cordova.plugin.ftp.upload("/sdcard/Ademi/" + "test.txt","/Android/mkdir/test.txt",  function(percent) {
                    if (percent == 1) {
                      alert("xtest: ftp: upload finish");
                      // cancel download after some time
                      //$timeout(function() {
                        //$window.cordova.plugin.ftp.cancel(function(ok) {
                          //$log.log("xtest: ftp: cancel ok=" + ok);
                        //}, function(error) {
                          //$log.log("xtest: ftp: cancel error=" + error);
                        //});
                      //}, 2000);
                      // 5. download remote file to local, you can rename at the same time. arg1: local file, arg2: remote file.
                      // make sure you can access and write the local dir.
                      window.cordova.plugin.ftp.download("/sdcard/Ademi/test2.txt", "/Android/test2.txt", function(percent) {
                        if (percent == 1) {
                          alert("xtest: ftp: download finish");
                          // 6. delete one file on ftp server
                          window.cordova.plugin.ftp.rm("/Android/mkdir/test.txt", function(ok) {
                            alert("xtest: ftp: rm ok=" + ok);
                            // 7. delete one dir on ftp server, note that just can be empty dir, or will fail
                            window.cordova.plugin.ftp.rmdir("/Android/mkdir", function(ok) {
                              alert("xtest: ftp: rmdir ok=" + ok);
                              alert("FIN");
                            }, function(error) {
                              alert("xtest: ftp: rmdir error=" + error);
                            });
                          }, function(error) {
                            alert("xtest: ftp: rm error=" + error);
                          });
                        } else {
                          $scope.pourcent = "xtest: ftp: download percent=" + percent*100 + "%";
                        }
                      }, function(error) {
                        alert("xtest: ftp: download error=" + error);
                      });
                    } else {
                      $scope.pourcent = "xtest: ftp: download percent=" + percent*100 + "%";
                    }
                  }, function(error) {
                    alert("xtest: ftp: upload error=" + error);
                  });
                }, function(error) {
                  alert("xtest: ftp: mkdir error=" + error);
                });
              }
            }, function(error) {
              alert("xtest: ftp: list error: " + error);
            });
          });
        } else {
          alert("xtest: ftp: not found!");
        }

      }

})