davidOhara
davidOhara

Reputation: 1008

Get File from Directory into NSData

I try to get my file in my self created Directory in the Documents Directory into a NSData Object like this:

NSString *path = [documentsDirectory stringByAppendingFormat:@"%@%@",@"/",fileName];

                NSError* errorr = nil;
                NSData *fileData = [NSData dataWithContentsOfFile:path options: 0 error: &errorr];
                if (fileData == nil)
                {
                    NSLog(@"Failed to read file, error %@", errorr);
                }
                else
                {

                }

But i always get this error:

Failed to read file, error Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x156be110 {NSFilePath=/var/mobile/Applications/679253E3-652C-45EE-B589-609E52E4E3B0/Documents/upload/test.xml, NSUnderlyingError=0x156ba7f0 "The operation couldn’t be completed. Permission denied"}

So if i check if the file is readable:

if ([[NSFileManager defaultManager] isReadableFileAtPath: path]  == YES)
                    NSLog (@"File is readable");
                else
                    NSLog (@"File is read only");

i get the result that the file is readable, so why do i get the error if i want to parse that file into NSData?!

UPDATE: i created my file like this:

NSString *filePath = [self dataFilePath:fileName];

[xmlData writeToFile:filePath atomically:YES];

- (NSString *)dataFilePath: (NSString *) path {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"upload"];

NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:NO attributes:nil error:&error];

NSString *documentsPath = [documentsDirectory
                           stringByAppendingPathComponent:path];
return documentsPath;

}

UPDATE2: After creating the file, i move it into another Directory with this function:

- (void)moveXmlFilesToUploadDirectory
{
@try {
    //Check if FILES_DIR exists
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [[paths objectAtIndex:0]     stringByAppendingPathComponent:FILES_DIR];//filesDir
    NSString *uploadDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:UPLOAD_DIR];//filesDir

    if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory])
    {
        NSString *extension = @"xml";
        NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
        NSEnumerator *e = [contents objectEnumerator];
        NSString *filename;
        while ((filename = [e nextObject])) {

            if ([[filename pathExtension] isEqualToString:extension]) {
                [[NSFileManager defaultManager] moveItemAtPath:documentsDirectory toPath:[uploadDirectory stringByAppendingPathComponent:filename] error:NULL];

            }
        }
    }

}
@catch (NSException *exception) {

}
@finally {

}

}

Maybe the Problem is after moving the file! Cause if i try to get my file into a NSData before i move it, everything works...

Upvotes: 1

Views: 4063

Answers (2)

Alexander Ney
Alexander Ney

Reputation: 781

It seems that you try to copy the whole directory to a destination meant to be a file. The result is that the filepath is a directory path in the end so you can't open it like a file which leads to the permission error message.

You should update your copy code to

 while ((filename = [e nextObject])) {

            if ([[filename pathExtension] isEqualToString:extension]) {
                [[NSFileManager defaultManager] moveItemAtPath:[documentsDirectory stringByAppendingPathComponent: filename] toPath:[uploadDirectory stringByAppendingPathComponent:filename] error:NULL];

            }
        }

to copy every file individually. That will create than the actual file at the destination path intend of a directory.

Upvotes: 3

Alexander Ney
Alexander Ney

Reputation: 781

The chances are high that you don't have the right permissions for that file. Please check the permissions and add read access for the current user or "everyone". You can set the read permissions for your user by the info dialog (cmd + i) or in the terminal with chmod u+r test.xml. Eventually you have to set the owner before with

sudo chown yourusername test.xml 

Hope that makes sense.

Upvotes: 1

Related Questions