【问题标题】:Download progress event is not working in ios using cordova plugin file transfer下载进度事件在使用cordova插件文件传输的ios中不起作用
【发布时间】:2017-09-25 15:51:13
【问题描述】:

我正在使用 cordova 文件传输插件从我的应用程序下载文件。

但是,当我使用进度事件向用户显示下载进度条时,每次显示 0% 时都无法正常工作。

这是我用来显示进度事件的代码......

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

谁能帮我解决这个ios下载进度条问题。

【问题讨论】:

    标签: javascript ios cordova phonegap-plugins file-transfer


    【解决方案1】:

    六个月前我也遇到过同样的问题,似乎是 Filetransfer 插件的错误。

    我找到的解决方案是进行此处提到的更改: https://issues.apache.org/jira/browse/CB-9936

    在 CDVFileTransfer.m 中,第 447 行到第 462 行之间的 download() 方法

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

    如果我用旧版本的代码替换它,那么它可以工作。

    delegate.connection = [NSURLConnection connectionWithRequest:req delegate:delegate];
    if (activeTransfers == nil) {
        activeTransfers = [[NSMutableDictionary alloc] init];
    }
    [activeTransfers setObject:delegate forKey:delegate.objectId];
    

    【讨论】:

    • Answers should contain more than just a link。请在答案中包含所有基本信息,并提供仅供参考的链接。
    • 我认为这个解决方案只有在构建过程中生成 *.ipa 后才能工作。但是,例如,使用 Phonegap Developer 应用程序对其进行测试呢?我们没有得到修复。所以我们不能在构建应用程序之前对其进行测试。我是对的还是有办法在仅提供应用程序并在 Phonegap Developer 应用程序/模拟器上预览它时使此修复工作?
    • Fran,您是如何实际应用该链接上描述的解决方案的?我按照那里的说明更改了 download() 方法中的代码,但仍然无法使其在 XCode 的 iPhone 模拟器中工作。就像@Meyer 说的那样,您能否使用示例代码编辑您的答案,以便我们可以尝试像您一样做?也许它也会为我们解决问题。
    【解决方案2】:
    function (progress) {
                        $timeout(function () {
                            $scope.downloadProgress = (progress.loaded / progress.total) * 100;
                            console.log($scope.downloadProgress)
                        });
                    }
    

    用这个函数替换你的进度函数,你可以在控制台查看你的结果

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 2021-01-09
      • 1970-01-01
      相关资源
      最近更新 更多