2015-09-03 2 views
2

Я хочу загрузить файл в определенной папке и вложенной папке.Ионные: Загрузка файла в пользовательский каталог

Код: - файл

var directoryPath = ""; 
    $ionicPlatform.ready(function() { 
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
      function (fileSystem) { 
       var directoryEntry = fileSystem.root; // to get root path to directory 
       console.log("directoryEntry="); 
       console.log(directoryEntry); 
       directoryEntry.getDirectory("XYZ/", { create: true, exclusive: false }, 
        function (Success) { 
         console.log("Directory Sucess"); console.log(Success); 
         Success.getDirectory("ABC/", { create: true, exclusive: false }, 
          function (SubDirectory) { 
           console.log(SubDirectory); 
           directoryPath = SubDirectory.nativeURL; 
           if ($localStorage.ProfileImage !== undefined && $localStorage.ProfileURL !== "") { 
            var ServerProfileFile = $localStorage.ProfileImage.substr($localStorage.ProfileImage.lastIndexOf('/') + 1); 
            var localProfileFile = ""; 
            if ($localStorage.ProfileURL !== undefined && $localStorage.ProfileURL !== "") { 
             localProfileFile = $localStorage.ProfileURL.substr($localStorage.ProfileURL.lastIndexOf('/') + 1); 
            } 
            if (ServerProfileFile !== localProfileFile) { 
             // var fp = ; 
             // console.log(fp); 
             var fileTransfer = new FileTransfer(); 

             fileTransfer.download(encodeURI($localStorage.ProfileImage), directoryPath + ServerProfileFile, 
              function (entry) { 
               console.log(entry); 
               console.log("download complete: " + entry.fullPath); 
               $localStorage.ProfileURL = entry.nativeURL; 
               ProfileImage.Image = $localStorage.ProfileURL; 

              }, 
              function (error) { 

               $localStorage.ProfileURL = $localStorage.ProfileImage; 
               ProfileImage.Image = $localStorage.ProfileURL; 

              }); 

            } else { 
             $cordovaFile.checkFile(directoryPath, localProfileFile) 
              .then(function (Filesuccess) { 

               ProfileImage.Image = $localStorage.ProfileURL; 

              }, function (error) { 

               var fileTransfer = new FileTransfer(); 

               fileTransfer.download(encodeURI($localStorage.ProfileImage), directoryPath + ServerProfileFile, 
                function (entry) { 
                 console.log(entry); 
                 console.log("download complete: " + entry.fullPath); 
                 $localStorage.ProfileURL = entry.nativeURL; 
                 ProfileImage.Image = $localStorage.ProfileURL; 

                }, 
                function (error) { 
               $localStorage.ProfileURL = $localStorage.ProfileImage; 
                 ProfileImage.Image = $localStorage.ProfileURL; 

                }); 

              }); 

            } 

           } 
           $scope.$apply(); 

          }, 
          function (SubErrror) { 
           console.log("Directory Fail SubErrror"); console.log(SubErrror); 
          }); 

        }, function (error) { 
         console.log("Directory Fail"); console.log(error); 
        }); 
      }, 
      function() { 
       console.log("error getting LocalFileSystem"); 
      }); 
    }); 

он загружает файл, но экономить на месте»: ///data/data/com.greatdevelopers.XYZ/files/files/XYZ/ABC/3705db1c- 5519-47f1-b716-c7c9324390aa_26183.jpeg « и я не нашел такого места в моем телефоне как внутри, так и во внешней памяти

, пожалуйста, скажите мне, где я делаю неправильно. Спасибо

+0

не имеют ответ для вас, но я предлагаю переработать ваш код, чтобы использовать ngCordova и обещания, а не обратные вызовы. Если бы я должен был работать с вашим кодом, я бы сразу разломил его и очистил. Если вы сделаете это, возможно, вы поймете, почему он не работает. – TheBosZ

ответ

0

Здесь мой рабочий код для загрузки файла в ионном пользовательском каталоге.

$ionicPlatform.ready(function() { 

    if(ionic.Platform.isIPad() || ionic.Platform.isAndroid() || ionic.Platform.isIOS()){ 

      fileTransferDir = cordova.file.dataDirectory; 
      var fileURL = fileTransferDir + 'test/test.jpg'; 

     // CREATE 
      $cordovaFile.createDir(fileTransferDir, "test", false) 
       .then(function (success) { 
       // success 
       console.log('dir created'); 
       console.log(success); 
       }, function (error) { 
       // error 
       console.log(error); 
       }); 

     // Download 

     var fileTransfer = new FileTransfer(); 
     var uri = encodeURI("http://ionicframework.com/img/ionic-logo-blog.png"); 

     fileTransfer.download(
      uri, 
      fileURL, 
      function(entry) { 
       console.log("download complete: " + entry.toURL()); 
       $scope.Path=fileURL; 
      }, 
      function(error) { 
       console.log("download error source " + error.source); 
       console.log("download error target " + error.target); 
       console.log("upload error code" + error.code); 
      } 
     ); 
    } 
}) 

Убедитесь, что добавить corvova файл плагина и впрыснуть в вашей ионной confix.xml

вы можете использовать переменную Path в шаблоне, чтобы показать, как файл:

<img src="{{Path}}">