javascript - 在单个进度条中显示下载多个文件的进度

标签 javascript angularjs algorithm file cordova

我正在为 Android 平台构建一个 ionic 应用程序。我正在使用 cordova 的 FileTransfer 插件下载文件。我可以下载多个文件,但无法为所有并发下载显示单个进度条。

在单个进度条中显示多个文件进度的正确方法是什么?下面是我的功能代码供引用:

Function to download multiple files by calling a single file download function in loop

$scope.downloadSongs = function () {
            angular.forEach($scope.album, function (song) {
                if (song.checked) {
                    $scope.downloadSong(song);
                }
            });
        };

Function to download a file

$scope.downloadSong = function (song) {
                $scope.downloads = $scope.downloads + 1;
                console.log("Downloads - " + $scope.downloads);
                var url = decodeURI(song.link);
                console.log(url);
                var filename = song.name + ".mp3";
                var folder = song.folder;
                var targetPath = cordova.file.externalRootDirectory + "musicbox/" + folder + "/" + filename;
                $cordovaFileTransfer.download(url, targetPath)
                    .then(function (entry) {
                        cordova.plugins.notification.local.schedule({
                            title: "Download Complete",
                            message: song.name
                        });
                    }, function (error) {
                        alert(JSON.stringify(error));
                    }, function (progress) {
                        $timeout(function () {
                            $scope.downloadProgress = (progress.loaded / progress.total) * 100;
                        })
                    });
            };

最佳答案

解决方案是在 View 中传递歌曲id

<li ng-repeat="song in songsList">
<progress id="{{song.id}}" max="100" value=""></progress>
</li>

比在 Controller 中

function(progress){
document.getElementById(song.id).value(Math.ceil((progress.loaded/progress.total)*100));
}

关于javascript - 在单个进度条中显示下载多个文件的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768229/

相关文章:

java - 简单递归算法中终止路径的问题

c++ - "How to impress interviewers with my coding? What practices can I adopt in the code I' 已经为给面试官留下深刻印象的问题而写了吗?

javascript - 菜单列表未在 Safari/iOS 中显示

javascript - 在对象/变量中存储对函数的引用

javascript - 将随机数传递给 Google 搜索

javascript - 触发事件点击页面加载

javascript - 如何处理手机 Angular 长按事件?

javascript - AngularJS - 在 PHP 中使用 Angular 路由提交表单不会传递表单参数

arrays - 给定一个二维矩阵,其中每列占用的 block 范围,找到每行占用的 block 数

javascript - 在 Codemirror 中获取选定范围