0

我正在尝试使用新的 Dropbox SDK 下载一个大文件。

我的下载代码是这样的-

DBUserClient *client = [DBClientsManager authorizedClient];

[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {    
        if(result){
            NSLog(@"File Downloaded");
            // open the file after being downloaded
        }
    }];

以前我使用 DBRESTClient 类的 loadMetadata 方法。

[restClient loadMetadata:path];

这反过来会触发其他一些委托方法,其中之一是

- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath

在这种方法中,我可以跟踪正在下载的文件的进度。

如何在 setResponse 块中跟踪我的下载进度?提前致谢。

4

2 回答 2

1

您可以使用setProgressBlock设置一个块来接收进度更新。这里有一个例子:

https://github.com/dropbox/dropbox-sdk-obj-c#download-style-request

于 2017-05-18T15:25:32.857 回答
0

主要归功于格雷格。

我正在使用 MBProgressHUD 直观地显示数据/图像/文件下载。

现在,在删除旧的 Dropbox SDK 的 DBRESTClient 委托后,这就是我的工作方式。

就我而言,首先,我使用以下代码列出了保管箱中的文件-

我正在将图像/文件下载到文档目录中。

为此,首先,我需要文件和文件夹的列表,因此,我通过调用getAllDropboxResourcesFromPath:path 是保管箱根目录的方法来获取列表。

在我的 viewDidLoad 方法中,我得到了列表。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getAllDropboxResourcesFromPath:@"/"];
     self.list = [NSMutableArray alloc]init; //I have the list array in my interface file
}

-(void)getAllDropboxResourcesFromPath:(NSString*)path{

    DBUserClient *client = [DBClientsManager authorizedClient];

    NSMutableArray  *dirList = [[NSMutableArray alloc] init];

    [[client.filesRoutes listFolder:path]
     setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderError *routeError, DBRequestError *networkError) {
         if (response) {
             NSArray<DBFILESMetadata *> *entries = response.entries;
             NSString *cursor = response.cursor;
             BOOL hasMore = [response.hasMore boolValue];

             [self listAllResources:entries];

             if (hasMore){
                 [self keepListingResources:client cursor:cursor];
             }
             else {
                 self.list = dirList;
             }
         } else {
             NSLog(@"%@\n%@\n", routeError, networkError);
         }
     }];
}

- (void)keepListingResources:(DBUserClient *)client cursor:(NSString *)cursor {
    [[client.filesRoutes listFolderContinue:cursor]
     setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderContinueError *routeError,
                        DBRequestError *networkError) {
         if (response) {
             NSArray<DBFILESMetadata *> *entries = response.entries;
             NSString *cursor = response.cursor;
             BOOL hasMore = [response.hasMore boolValue];

             [self listAllResources:entries];

             if (hasMore) {
                 [self keepListingResources:client cursor:cursor];
             }
             else {
                 self.list = dirList;
             }
         } else {
             NSLog(@"%@\n%@\n", routeError, networkError);
         }
     }];
}

- (void) listAllResources:(NSArray<DBFILESMetadata *> *)entries {
    for (DBFILESMetadata *entry in entries) {
           [dirList addObject:entry];    
    }
}

上面的代码将所有文件和文件夹作为DBFILESMetadata类型对象存储在列表数组中。

现在我准备好下载了,但是我的文件很大,所以我需要显示我正在使用的下载进度MBProgressHUD

-(void)downloadOnlyImageWithPngFormat:(DBFILESMetadata *)file{

 //adding the progress view when download operation will be called
 self.progressView = [[MBProgressHUD alloc] initWithWindow:[AppDelegate window]]; //I have an instance of MBProgressHUD in my interface file
 [[AppDelegate window] addSubview:self.progressView];

 //checking if the content is a file and if it has png extension
 if ([file isKindOfClass:[DBFILESFileMetadata class]] && [file.name hasSuffix:@".png"]) {

        NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString* localPath = [documentsPath stringByAppendingPathComponent:file.pathLower];
        BOOL exists=[[NSFileManager defaultManager] fileExistsAtPath:localPath];
        if(!exists) {
            [[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        NSURL *documentUrl = [NSURL fileURLWithPath:localPath];
        NsString *remotePath = file.pathLower;
        DBUserClient *client = [DBClientsManager authorizedClient];
        [[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {
        if(result){
            NSLog(@"File Downloaded");
        }
    }] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
        [self setProgress:[self calculateProgress:totalBytesExpectedToDownload andTotalDownloadedBytes:totalBytesDownloaded]];
    }];
}

- (void) setProgress:(CGFloat) progress {
   [self.progressView setProgress:progress];
}
- (CGFloat) calculateProgress:(long long)totalbytes andTotalDownloadedBytes:(long long)downloadedBytes{
    double result = (double)downloadedBytes/totalbytes;
    return result;
}

希望这可以帮助其他人。再次非常感谢 Greg 给我的提示。

于 2017-05-19T09:28:55.890 回答