我有以下代码用于从 iphone 的文档目录中获取文件:
NSString *docsDir;
NSString *realpath;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
realpath=[[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"2_program.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: realpath ] == YES)
{
NSLog(@"find file");
NSData *uploadedData=[NSData dataWithContentsOfFile:realpath];
NSString * uploadedDataBase64= [NSString base64forData:uploadedData];
NSLog(@"base64: %@",uploadedDataBase64);
}
else
{
NSLog(@"not found");
}
filemanager 找到文件但 nsdata 返回 null 但是它们都得到了相同的路径
我的文件大小约为 60kb
任何想法为什么会发生?我错过了什么吗?
最佳答案
尝试 this Matt Gallagher 的 NSData+Base64 类别以及下面的代码
NSString *realpath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent: @"2_program.db"];
if ([[NSFileManager defaultManager] fileExistsAtPath:realpath]){
NSLog(@"find file");
NSData *uploadedData=[NSData dataWithContentsOfFile:realpath];
NSString *uploadedDataBase64=[[[NSString alloc]initWithData:uploadedData encoding:NSUTF8StringEncoding] base64EncodedString];
NSLog(@"base64: %@",uploadedDataBase64);
}
else{
NSLog(@"not found");
}
关于ios - NSData datawithcontentsofffile 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23993289/