简体   繁体   中英

Download progress event is not working in ios using cordova plugin file transfer

I am using cordova file transfer plugin to download files from my app.

But, when I m using progress event to show the user to progress bar on downloading It is not working properly every time it shows 0%.

Here is code which I used to show progress event....

$cordovaFileTransfer.download(url, targetPath, downloadOptions,
    trustHosts).then(function(result) {
}, function(err) {
}, function(progress) {
    if (progress.lengthComputable) {
        $rootScope.downloadProgress = (progress.loaded / progress.total) * 100;
    } else {
        $rootScope.downloadProgress = (progress.loaded / file_size) * 100;
    }
});

Can anyone please help me out to solve this downloading progress bar issue in ios.

I had the same problem six months ago, it seems to be a bug in the Filetransfer plugin.

The solution I found was to do the changes mentioned here: https://issues.apache.org/jira/browse/CB-9936

In CDVFileTransfer.m, download() method between line 447 to 462

 delegate.connection = [[NSURLConnection alloc] initWithRequest:req delegate:delegate startImmediately:NO]; if (self.queue == nil) { self.queue = [[NSOperationQueue alloc] init]; } [delegate.connection setDelegateQueue:self.queue]; @synchronized (activeTransfers) { activeTransfers[delegate.objectId] = delegate; } // Downloads can take time // sending this to a new thread calling the download_async method dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { [delegate.connection start];} ); 

If I replaced it with the older version of the code, then it works.

 delegate.connection = [NSURLConnection connectionWithRequest:req delegate:delegate]; if (activeTransfers == nil) { activeTransfers = [[NSMutableDictionary alloc] init]; } [activeTransfers setObject:delegate forKey:delegate.objectId]; 
function (progress) {
                    $timeout(function () {
                        $scope.downloadProgress = (progress.loaded / progress.total) * 100;
                        console.log($scope.downloadProgress)
                    });
                }

Replace your progress function with this function, u can check your result in console

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM