版权所有,禁止匿名转载;禁止商业使用。
类似于QQ中收到的文件,可预览,但是这是本地文件,没有上传下载的功能。。
思路:先用tableView加载显示本地文件,然后用文本查看器实现预览功能,也可用其他应用打开本文件。
核心代码如下:
NSFileManager *fileManager = [NSFileManager defaultManager];
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docDirs = [paths objectAtIndex:0];
NSError *error = nil;
//_dataFileArray是包含有该文件夹下所有文件的文件名及文件夹名的数组
_dataFileArray = [fileManager contentsOfDirectoryAtPath:docDirs error:&error];
fileList = [NSMutableArray array];
XCPFileObject *fileObj;
for (NSString *name in _dataFileArray) {
fileObj = [[XCPFileObject alloc] init];
fileObj.fileName = name;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:[docDirs stringByAppendingPathComponent:name] error:nil];
fileObj.filemTime = [fileAttributes objectForKey:@"NSFileCreationDate"];
fileObj.fileSize = [[fileAttributes objectForKey:@"NSFileSize"] integerValue];
fileObj.fileOwner = [fileAttributes objectForKey:@"NSFileGroupOwnerAccountName"];
[fileList addObject:fileObj];
}
//列出给定一个文件夹里的所有子文件夹名
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
BOOL isDir = NO;
for (NSString *file in _dataFileArray) {
NSString *path = [docDirs stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if (isDir) {
[dirArray addObject:file];
}
isDir = NO;
}
封装好的Dome链接:不足的地方还请大家多多指教
思路:先用tableView加载显示本地文件,然后用文本查看器实现预览功能,也可用其他应用打开本文件。
核心代码如下:
NSFileManager *fileManager = [NSFileManager defaultManager];
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docDirs = [paths objectAtIndex:0];
NSError *error = nil;
//_dataFileArray是包含有该文件夹下所有文件的文件名及文件夹名的数组
_dataFileArray = [fileManager contentsOfDirectoryAtPath:docDirs error:&error];
fileList = [NSMutableArray array];
XCPFileObject *fileObj;
for (NSString *name in _dataFileArray) {
fileObj = [[XCPFileObject alloc] init];
fileObj.fileName = name;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:[docDirs stringByAppendingPathComponent:name] error:nil];
fileObj.filemTime = [fileAttributes objectForKey:@"NSFileCreationDate"];
fileObj.fileSize = [[fileAttributes objectForKey:@"NSFileSize"] integerValue];
fileObj.fileOwner = [fileAttributes objectForKey:@"NSFileGroupOwnerAccountName"];
[fileList addObject:fileObj];
}
//列出给定一个文件夹里的所有子文件夹名
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
BOOL isDir = NO;
for (NSString *file in _dataFileArray) {
NSString *path = [docDirs stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if (isDir) {
[dirArray addObject:file];
}
isDir = NO;
}
封装好的Dome链接:不足的地方还请大家多多指教