如何以编程方式获取ios设备中的文件列表/上传/重命名/写入/读取文件?

时间:2016-01-08 12:18:35

标签: ios objective-c file file-upload

我正在创建一个应用程序,用户必须上传文件和图像,如xls,pdf,txt,jpg,png等。我想向用户显示他的iOS设备中存在的所有文件请帮助我任何一个

2 个答案:

答案 0 :(得分:0)

iOS中无法实现您的目标。您创建的应用程序只能访问其Documents文件夹中的文件。

没有“来自手机的所有文件”概念,每个应用程序都管理它自己的文件。您可以与其他应用程序交互的唯一方法是通过应用程序开发人员提供的公共API。

如果要获取Documents目录中的所有文件,可以通过这种方式获取路径:

{% for entry in context.entries %}

您还可以使用ALAssets(最多iOS7)或PHAssets(iOS 8及更高版本)访问用户的照片库。

希望这有帮助。

答案 1 :(得分:0)

首先,您应该阅读Apple文档中的 NSFileManager 概念,然后自动知道如何执行此操作::

您可以访问的内容仅限于您的应用内,仅此而已 -

您能否看到以下代码?我希望它会对你有所帮助

(1).  #pragma mark
#pragma mark -- list all the files exists in Document Folder in our Sandbox.
- (void)listAllLocalFiles{
// Fetch directory path of document for local application.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// NSFileManager is the manager organize all the files on device.
NSFileManager *manager = [NSFileManager defaultManager];
// This function will return all of the files' Name as an array of NSString.
NSArray *files = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
// Log the Path of document directory.
NSLog(@"Directory: %@", documentsDirectory);
// For each file, log the name of it.
for (NSString *file in files) {
    NSLog(@"File at: %@", file);
}
}

(2). #pragma mark
    #pragma mark -- Create a File in the Document Folder.
 - (void)createFileWithName:(NSString *)fileName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

NSFileManager *manager = [NSFileManager defaultManager];
// 1st, This funcion could allow you to create a file with initial contents.
// 2nd, You could specify the attributes of values for the owner, group, and permissions.
// Here we use nil, which means we use default values for these attibutes.
// 3rd, it will return YES if NSFileManager create it successfully or it exists already.
if ([manager createFileAtPath:filePath contents:nil attributes:nil]) {
    NSLog(@"Created the File Successfully.");
} else {
    NSLog(@"Failed to Create the File");
}
}

(3). #pragma mark
     #pragma mark -- Delete a File in the Document Folder.
   - (void)deleteFileWithName:(NSString *)fileName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Have the absolute path of file named fileName by joining the document path with fileName, separated by path separator.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

NSFileManager *manager = [NSFileManager defaultManager];
// Need to check if the to be deleted file exists.
if ([manager fileExistsAtPath:filePath]) {
    NSError *error = nil;
    // This function also returnsYES if the item was removed successfully or if path was nil.
    // Returns NO if an error occurred.
    [manager removeItemAtPath:filePath error:&error];
    if (error) {
        NSLog(@"There is an Error: %@", error);
    }
} else {
    NSLog(@"File %@ doesn't exists", fileName);
}
}

(4). #pragma mark
     #pragma mark -- Rename a File in the Document Folder.
     - (void)renameFileWithName:(NSString *)srcName toName:(NSString *)dstName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathSrc = [documentsDirectory stringByAppendingPathComponent:srcName];
NSString *filePathDst = [documentsDirectory stringByAppendingPathComponent:dstName];
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePathSrc]) {
    NSError *error = nil;
    [manager moveItemAtPath:filePathSrc toPath:filePathDst error:&error];
    if (error) {
        NSLog(@"There is an Error: %@", error);
    }
} else {
    NSLog(@"File %@ doesn't exists", srcName);
}
}

(5).#pragma mark
    #pragma mark -- Read a File in the Document Folder.
   /* This function read content from the file named fileName.
   */
   - (void)readFileWithName:(NSString *)fileName{
// Fetch directory path of document for local application.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Have the absolute path of file named fileName by joining the document path with fileName, separated by path separator.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

// NSFileManager is the manager organize all the files on device.
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]) {
    // Start to Read.
    NSError *error = nil;
    NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSStringEncodingConversionAllowLossy error:&error];
    NSLog(@"File Content: %@", content);

    if (error) {
        NSLog(@"There is an Error: %@", error);
    }
} else {
    NSLog(@"File %@ doesn't exists", fileName);
}
}

(6). #pragma mark
     #pragma mark -- Write a File in the Document Folder.
     /* This function Write "content" to the file named fileName.
      */
   - (void)writeString:(NSString *)content toFile:(NSString *)fileName{
// Fetch directory path of document for local application.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Have the absolute path of file named fileName by joining the document path with fileName, separated by path separator.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

// NSFileManager is the manager organize all the files on device.
NSFileManager *manager = [NSFileManager defaultManager];
// Check if the file named fileName exists.
if ([manager fileExistsAtPath:filePath]) {
    NSError *error = nil;
    // Since [writeToFile: atomically: encoding: error:] will overwrite all the existing contents in the file, you could keep the content temperatorily, then append content to it, and assign it back to content.
    // To use it, simply uncomment it.
    //        NSString *tmp = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:NSStringEncodingConversionAllowLossy error:nil];
    //        if (tmp) {
    //            content = [tmp stringByAppendingString:content];
    //        }
    // Write NSString content to the file.
    [content writeToFile:filePath atomically:YES encoding:NSStringEncodingConversionAllowLossy error:&error];
    // If error happens, log it.
    if (error) {
        NSLog(@"There is an Error: %@", error);
    }
} else {
    // If the file doesn't exists, log it.
    NSLog(@"File %@ doesn't exists", fileName);
}

// This function could also be written without NSFileManager checking on the existence of file,
// since the system will atomatically create it for you if it doesn't exist.
  }