ios - 文件没有写入文档目录?

标签 ios objective-c google-drive-api download

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

最佳答案

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

 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);
    }
   }];

关于ios - 文件没有写入文档目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206908/

相关文章:

google-drive-api - 使用自定义域发布 Google Drive 网站

javascript - 如何让 Google Drive API 示例在 Phonegap 应用程序中运行(支持 Android)?

ios - 如何禁用 UITableView 中所有未选中的单元格

ios - Objective-C : How to make a cache without keys

iOS内联if else编译错误: "Expected : "; "Expected expression"

objective-c - 是否应该尽可能避免反转 NSMutableArray?

google-apps-script - 通过 Google Apps 脚本发布 Google 电子表格

java - 如何在基于 Java 的应用程序服务器上使用 GCM 向 iOS 设备发送推送通知

iOS:如何在使用 componentSeparatedByCharactersInSet 时维护分隔符

ios - 从 objective-c 中的 nsstring 获取最后一个空格