0

// 我正在尝试从谷歌驱动器下载文件我能够在 nslog 中列出文件及其大小,但无法写入文档目录。目前它保存带有扩展名的路径但不保存原始文件。

GTLDriveFile *file;
NSString *downloadedString = file.downloadUrl; // file is GTLDriveFile
GTMHTTPFetcher *fetcher = [self.driveService.fetcherService  fetcherWithURLString:downloadedString];
filename=[[NSString alloc]init];


[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error)     
{
 GTLDriveFile *file = [driveFiles objectAtIndex:indexPath.row];

 filename = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"\n\n\n\n\n");    
    NSLog(@"This is File Size=====>%@",file.fileSize);

    NSLog(@"This is file Name===>%@",file.title);

    if(file.downloadUrl!= nil)
        {

       filename=file.title;    

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSString *documentsDirectory = [paths objectAtIndex:0];

  documentsDirectory = [[paths objectAtIndex:0]stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename]];
[data writeToFile:documentsDirectory atomically:YES encoding:NSUTF8StringEncoding error:nil];


    NSLog(@"my path:%@",documentsDirectory);
    }
    else
    {
    NSLog(@"Error - %@", error.description);
    }
}];
// ya i got it...
 NSString *downloadURL=[[self.driveFiles objectAtIndex:indexPath.row] downloadUrl];
 GTMHTTPFetcher *fetcher =

 [self.driveService.fetcherService fetcherWithURLString:downloadURL];
4

2 回答 2

1

正确的代码应如下所示,请参阅我在所有大写字母中的评论。

GTLDriveFile *file;
NSString *downloadedString = file.downloadUrl; // file is GTLDriveFile
GTMHTTPFetcher *fetcher = [self.driveService.fetcherService  fetcherWithURLString:downloadedString];
filename=[[NSString alloc]init];


[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error)     
{
 GTLDriveFile *file = [driveFiles objectAtIndex:indexPath.row];

    //filename = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  //THIS LINE IS WRONG

    NSLog(@"\n\n\n\n\n");    
    NSLog(@"This is File Size=====>%@",file.fileSize);

    NSLog(@"This is file Name===>%@",file.title);

    if(file.downloadUrl!= nil)
        {

       filename=file.title;  // THIS IS CORRECT  

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSString *documentsDirectory = [paths objectAtIndex:0];

  documentsDirectory = [[paths objectAtIndex:0]stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename]];

//USE DATA TO WRITE THE FILE NOT `documentsDirectory`
[data writeToFile:documentsDirectory atomically:YES];


    NSLog(@"my path:%@",documentsDirectory);
    }
    else
    {
    NSLog(@"Error - %@", error.description);
    }
}];

编辑

你也可以参考这个链接

于 2013-08-13T10:43:08.893 回答
1

// 我试过这个.. 它对我有用...

 NSString *downloadURL=[[self.driveFiles objectAtIndex:indexPath.row] downloadUrl];
 GTMHTTPFetcher *fetcher =

 [self.driveService.fetcherService fetcherWithURLString:downloadURL];

 filename=[[NSString alloc]init];   

 [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error)

 {
    GTLDriveFile *file = [driveFiles objectAtIndex:indexPath.row]; 

    NSLog(@"%@",file.fileSize);
    NSLog(@"%@",file.title);

    if(file.downloadUrl!= nil)
        {

          if (data!=nil) 

            {
       filename=file.title;    

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

documentsDirectory = [[paths objectAtIndex:0]stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",filename]];

 [data writeToFile:documentsDirectory atomically:YES];

  NSLog(@"my path:%@",documentsDirectory);

   }
   }
   else
   {
        NSLog(@"Error - %@", error.description);
    }
   }];
于 2013-08-13T11:47:03.703 回答