Ionic:文件在自定义目录中下载

时间:2015-09-03 08:14:19

标签: cordova ionic-framework ionic ngcordova

我想在特定文件夹和子文件夹中下载文件。

代码: -

  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");
            });
    });

它正在下载文件,但保存在该位置" file:///data/data/com.greatdevelopers.XYZ/files/files/XYZ/ABC/3705db1c-5519-47f1-b716-c7c9324390aa_26183。 JPEG" 我没有在手机内部和外部存储器中找到这样的位置

请告诉我我在哪里做错了。感谢

1 个答案:

答案 0 :(得分: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}}">