离子应用程序中打开文件

时间:2016-03-22 10:39:59

标签: javascript file ionic-framework

如何在我的设备中导航trhoug文件并在离子应用程序中打开它?我遵循这两个教程都工作,但在第一个我无法打开文件,我不知道如何插入第二个打开文件。请帮忙 FIRSTSECOND

我的代码

 var app=angular.module('starter', ['ionic','ngCordova'])
    .run(function($ionicPlatform) {
      $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
          // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
          // for form inputs)
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

          // Don't remove this line unless you know what you are doing. It stops the viewport
          // from snapping when text inputs are focused. Ionic handles this internally for
          // a much nicer keyboard experience.
          cordova.plugins.Keyboard.disableScroll(true);
        }
        if(window.StatusBar) {
          StatusBar.styleDefault();
        }
      });
    });

    app.controller("ExampleController", function($scope, $cordovaFile, $ionicPlatform, $fileFactory, $cordovaPrinter, $cordovaFileOpener2, $interval, $rootScope) {

      $scope.openPDF= function() {
              $cordovaFileOpener2.open(
                fileSystem.name,
                'application/pdf'
              ).then(function() {
                  console.log('Success');
              }, function(err) {
              console.log('An error occurred: ' + JSON.stringify(err));
              });
          };

      var fs = new $fileFactory();

      $ionicPlatform.ready(function() {
        fs.getEntriesAtRoot().then(function(result) {
          console.log("result "+ JSON.stringify(result));
          $scope.files = result;
        }, function(error) {
          console.error(error);
        });

        $scope.getContents = function(path) {
          fs.getEntries(path).then(function(result) {
            console.log("result "+JSON.stringify(result));
        //    console.log("result "+result);
            $scope.files = result;
            $scope.files.unshift({name: "[parent]"});
            //funzione per trovare il padre del path corrente
            fs.getParentDirectory(path).then(function(result) {
            //  console.log("result "+result);
              console.log("result "+JSON.stringify(result));
              result.name = "[parent]";
              $scope.files[0] = result;
    /*
              $cordovaFileOpener2.open(
              fileSystem.fullPath,
              'application/pdf'
              ).then(function() {
                  console.log('Success');
              }, function(err) {
              console.log('An error occurred: ' + JSON.stringify(err));
              });

                deferred.resolve(entries);
              console.log("entries "+ JSON.stringify(entries));
    */

            });
          }, function(error){
            console.error(error);
          });
        }
      });

    });


    app.factory("$fileFactory", function($q, $cordovaFileOpener2) {

      var File = function() {};

      File.prototype = {

        getParentDirectory: function(path) {
          //questa è la promise
          var deferred = $q.defer();
          //accedo al file local e prendo il path
          window.resolveLocalFileSystemURL(path, function(fileSystem) {
            fileSystem.getParent(function(result) {
              console.log("result "+JSON.stringify(result));
              deferred.resolve(result);
            }, function(error) {
              deferred.reject(error);
            });
          }, function(error) {
            deferred.reject(error);
          });
          return deferred.promise;
        },

        //per trovare tutti i file e cartelle nella root del device
        getEntriesAtRoot: function() {
          var deferred = $q.defer();
          window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
                console.log(fileSystem.name);
            var directoryReader = fileSystem.root.createReader();
            directoryReader.readEntries(function(entries) {
          //    console.log("entries "+entries);
              console.log("result "+ JSON.stringify(entries));
              deferred.resolve(entries);
            }, function(error) {
              deferred.reject(error);
            });
          }, function(error) {
            deferred.reject(error);
          });
          return deferred.promise;
        },

        //per trovare tutti i files e cartelle nel path dato
        getEntries: function(path) {
          var deferred = $q.defer();
          window.resolveLocalFileSystemURL(path, function(fileSystem) {
            console.log(fileSystem.name);
            console.log(fileSystem.fullPath);

            var directoryReader = fileSystem.createReader();
            console.log("directoryReader "+JSON.stringify(directoryReader));
            directoryReader.readEntries(function(entries) {
            //  console.log("entries "+entries);
    /*
            $cordovaFileOpener2.open(
            fileSystem.fullPath,
            'application/pdf'
            ).then(function() {
                console.log('Success');
            }, function(err) {
            console.log('An error occurred: ' + JSON.stringify(err));
            });

              deferred.resolve(entries);
            console.log("entries "+ JSON.stringify(entries));
    */
            }, function(error) {
              deferred.reject(error);
            });

          }, function(error) {
            deferred.reject(error);
          });
          return deferred.promise;
        }

      };

      return File;

    });

0 个答案:

没有答案