如何从谷歌驱动器部分下载文件使用目标c?

时间:2014-03-21 09:57:36

标签: objective-c google-drive-api

我想从谷歌驱动器使用目标下载一个大文件,我使用google-api-objectivec-client-read-only lib.But当我下载大文件时,我看到内存繁荣。 例如,文件的大小为589M,程序的内存超过600M。所以我想从谷歌驱动器部分下载文件。 我的代码:

-(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
    {

    }
}

如何修改从谷歌驱动器部分下载文件的方法?

1 个答案:

答案 0 :(得分:2)

好的,我解决了。它很容易。 我将代码修改为:

-(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
    {

    }
}