Karl Doenitz
Karl Doenitz

Reputation: 2230

how to partial download a file from google drive use objective c?

I wanna download a big file from google drive use objective,and I use the google-api-objectivec-client-read-only lib.But when I download the big file,I see the memory boom up. For example,the file's size is 589M,the program's memory more than 600M.So I wannna partial download the file from google drive. My code:

-(void)getFileMetadataWithService:(NSString *)fileId
{
    GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:fileId];
    [service executeQuery:query
        completionHandler:^(GTLServiceTicket *ticket, GTLDriveFile *file,
                            NSError *error) {
            if (error == nil) {
                NSLog(@"Title: %@", file.title);
                NSLog(@"Description: %@", file.descriptionProperty);
                NSLog(@"MIME type: %@", file.mimeType);
                NSLog(@"download url:%@",file.downloadUrl);
                NSLog(@"export link:%@",file.exportLinks);
                [self downloadFileContentWithService:@"example"
                                                file:file];
            } else {
                NSLog(@"An error occurred: %@", error);
            }
        }];
}


-(void)downloadFileContentWithService:(NSString *)loaclpath
                                 file:(GTLDriveFile *)file
{
    NSLog(@"download file");
    if (file.downloadUrl != nil)
    {
        NSLog(@"begin download");
        GTMHTTPFetcher *fetcher =[service.fetcherService fetcherWithURLString:file.downloadUrl];
        [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
            if (error == nil) {
                [data writeToFile:@"/Users/mycomputer/Desktop/a.mov" atomically:YES];
                [data writeToFile:@"/Users/mycomputer/Desktop/b.mov" atomically:YES];
                [data writeToFile:@"/Users/mycomputer/Desktop/c.mov" atomically:YES];
                NSLog(@"download ok");
            } else {
                NSLog(@"An error occurred: %@", error);
            }
        }];
    }
    else
    {

    }
}

How to modify the method to partial download a file from google drive?

Upvotes: 1

Views: 1295

Answers (1)

Karl Doenitz
Karl Doenitz

Reputation: 2230

OK,I fix it up.It is so easy. I modify the code as:

-(void)downloadFileContentWithService:(NSString *)loaclpath
                                 file:(GTLDriveFile *)file
{
    NSLog(@"download file");
    if (file.downloadUrl != nil)
    {
        NSLog(@"begin download");
        GTMHTTPFetcher *fetcher =[service.fetcherService fetcherWithURLString:file.downloadUrl];
        fetcher.downloadPath = @"/Users/KarlDoenitz/Desktop/download/bisaishipin.mov";
        [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
            if (error == nil) {
                NSLog(@"download ok");
            } else {
                NSLog(@"An error occurred: %@", error);
            }
        }];
    }
    else
    {

    }
}

Upvotes: 2

Related Questions