How to get List of files/Upload/rename/write/read

2019-09-03 09:49发布

I am creating an app in which user will have to upload the files and images like xls, pdf, txt, jpg, png etc. I want to show the user all the files present in his iOS device please help me any one.

2条回答
看我几分像从前
2楼-- · 2019-09-03 10:12

First of all you should read NSFileManager concept in Apple Documentation then automatically you should know how to do this::

what you can access is within your app only, nothing more –

Can you please see the following code . i hope it will be helpful to you

(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.
  }
查看更多
等我变得足够好
3楼-- · 2019-09-03 10:15

What you want to is not possible in iOS. An application you create only has access to files in it's Documents folder.

There is no "all files from the phone" notion, each application manages it's own files. The only way you can interact with other applications is through a public API provided by the application developers.

If you want to get all the files inside your Documents directory you can get the path this way:

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [searchPaths objectAtIndex:0];

You also have access to the user's photo library with which you can interact using ALAssets (up to iOS7) or PHAssets (iOS 8 and up).

Hope this helps.

查看更多
登录 后发表回答