显示下载进度条离子

时间:2016-05-12 02:58:53

标签: android angularjs cordova ionic-framework progress-bar

我正在开发一款离子应用程序。我正在使用cordova的FileTransfer插件来下载pdf文件。我可以将文件下载到我的内部存储器,但无法显示downloaidng的单个进度条。 显示下载进度的最佳方式是什么。

控制器

var url = 'http://someurl.com/api/pdf_download/' + id;
// Android
var targetPath = 'file:///storage/sdcard0/' + id + '.pdf';
var trustHosts = true;
var options = {};
$cordovaFileTransfer.download(url, targetPath, options, trustHosts)
    .then(function(result) {
        console.log(result);
    }, function() {
        var alertPopup = $ionicPopup.alert({
            title: 'No internet access',
            buttons: [{
                text: 'OK',
                type: 'button-assertive'
            }]
        });
        alertPopup.then(function() {});

    }, function(progress) {
        $timeout(function() {
            $scope.downloadProgress = (progress.loaded / progress.total) * 100;
        })
        console.log('progress--->', $scope.downloadProgress);
    });

2 个答案:

答案 0 :(得分:1)

我已使用cordovaToast插件来显示此功能。这是显示pdf下载进度的示例

<强> HTML

<ion-view >
    <div class="bar bar-subheader bar-positive" style="padding:0px;height: 8px;" >
        <progress id="progressbar" max="100" value="{{ downloadProgress }}" class="progress"> </progress>
    </div>
    <ion-content>
    </ion-content>
</ion-view>

<强> CSS

.progress {
    margin: 0 px;
    height: 8 px;
    background - color: #F1292B!important;
    border - radius: 2 px;
    box - shadow: 0 2 px 5 px rgba(0, 0, 0, 0.25) inset;
}

<强> JS

if (window.cordova) {
    var url = '{{base_url}}/pdf_download/' + id;
    // Android
    var targetPath = 'file:///storage/sdcard0/' + 'fpl_' + id + '.pdf';
    var trustHosts = true;
    var options = {};
    $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
        .then(function(result) {
            $cordovaToast
                .show('File downloaded successfully..', 'short', 'center')
                .then(function() {
                    // success
                }, function() {
                    // error
                });

            console.log(result);
        }, function() {
            var alertPopup = $ionicPopup.alert({
                title: 'No internet access',
                buttons: [{
                    text: 'OK',
                    type: 'button-assertive'
                }]
            });
            alertPopup.then(function() {});

        }, function(progress) {
            var dnldpgs = progress.loaded.toString().substring(0, 2);
            $scope.downloadProgress = parseInt(dnldpgs);
        });
}

答案 1 :(得分:0)

由于benka已经回答了我的问题,你应该使用<progress> html元素。

完整的答案可以在这里找到 - &gt; https://stackoverflow.com/a/25553044/3671726