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