文件没有写入文档目录?

时间:2013-08-13 10:38:55

标签: ios objective-c google-drive-api downloading

//我正在尝试从谷歌驱动器下载文件我能够在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];

2 个答案:

答案 0 :(得分: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);
    }
}];

修改

您可以参考此link

答案 1 :(得分: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);
    }
   }];