简体   繁体   中英

Reading file's content downloaded from dropbox -objective-c

I want to read and print the file's content which downloaded from dropbox but my "readFile" method prints null. I am sure the file is downloaded successfully.

-(void)download
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", @"File.txt"]];


    [[self restClient] loadFile:@"/File.txt" intoPath:filePath];

}
- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
       contentType:(NSString*)contentType metadata:(DBMetadata*)metadata {

    [self readFile:@"File.txt"];
    NSLog(@"File loaded into path: %@", localPath);
}

-(void)readFile:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileNameData=[NSString stringWithFormat:@"%@",fileName];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileNameData];
    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

    NSError *error;

    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

    NSLog(@"%@", error);
}

and this is output:

---------******** CONTENT OF THE DOWNLOADED FILE *********------------(null)

I updated my code for capture erorr and I am getting this error from stringWithContentsOfFile:

Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x1669c2b0 {NSFilePath=/var/mobile/Applications/11B10727-E372-1147-26BD-1D24S60B8E54/Docume‌​nts/File.txt, NSStringEncoding=4} 2013-08-05 22:06:03.229 DBApp[496:60b]

It looks like your code is reading a file called "File.txt" rather than the actual file that was downloaded from Dropbox. Am I missing something?

EDIT

Based on the comments below, it looks like the error is 261, related to string encoding. You might want to try a different encoding or ensure that the text file is encoded the way you expect it to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM