【问题标题】:Ionic: File Download in custom directory离子:自定义目录中的文件下载
【发布时间】:2015-11-28 23:27:24
【问题描述】:

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

代码:-

  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”位置 而且我在手机内部和外部存储器中都没有找到这样的位置

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

【问题讨论】:

  • 我没有给你答案,但我建议你修改你的代码以使用 ngCordova 和 promises 而不是回调。如果我要使用您的代码,我会立即将其分解并清理干净。如果你这样做,你可能会弄清楚它为什么不起作用。

标签: cordova ionic-framework ionic ngcordova


【解决方案1】:

这是我在 ionic 自定义目录中下载文件的工作代码。

$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 文件插件并注入您的 ionic confix.xml

您可以在模板中使用变量Path来显示文件,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2011-02-16
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多