2017-05-18 7 views
0

새 Dropbox SDK로 큰 파일을 다운로드하려고합니다.filesRoutes의 downloadUrl : overwrite : destination : 메소드로 새 Dropbox SDK (V2)를 사용하여 진행 상황을 추적하십시오.

내 다운로드 코드는

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 방법을 사용하는 데 사용 this- 같은 것입니다. 이 방법

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

을 그 중 하나 차례로 다른 대리자 메서드를 트리거 할

[restClient loadMetadata:path]; 

는 내가 다운로드 할 파일의 진행 상황을 추적 할 수 있습니다.

setResponse 블록에서 다운로드 진행률을 어떻게 추적합니까? 미리 감사드립니다.

답변

1

setProgressBlock을 사용하면 진행률 업데이트를받을 블록을 설정할 수 있습니다. 예를 들어 여기에있다 :

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

+0

감사합니다. 다른 사람들의 hep에서는 MBProgressHUD에 시각적으로 표시 할 백분율로 바이트를 설정하는 방법에 대한 샘플 코드를 추가 할 수 있습니다. 그냥 제안. 고마워. 나는 당신의 제안과 함께 작동시켰다. – Natasha

0

홈페이지 신용 그렉 간다.

MBProgressHUD를 사용하여 데이터/이미지/파일 다운로드를 시각적으로 표시하고 있습니다.

이제 기존 Dropbox SDK의 DBRestClient 대리자를 제거한 후 이것이 어떻게 작동하는지 확인하는 방법입니다. 내 경우

는, 첫째, 나는 문서 디렉토리에 이미지/파일을 다운로드하고 다음과 같은 코드 -

으로 드롭 박스에서 파일을 나열하고있다.

먼저 파일과 폴더 목록이 필요합니다. 따라서메서드를 호출하여 목록을 가져옵니다. 여기서 경로는 보관 용 계정의 루트입니다.

내 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; 
} 

희망이 있으면 도움이 될 것입니다. 나에게 힌트를 줘서 그렉에게 다시 큰 감사.