這裏是我的代碼下載一本書不能下載IOS文件
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
NSString *filePath= [resourceDocPath stringByAppendingPathComponent:_bookname];//in _bookname i am already getting the value ,say abcd.pdf
NSLog(@"The bookfilepath is %@",filePath);
NSLog(@"the lenght of recieved data is %d", _recievedData.length);//here i get the correct file size also
[_recievedData writeToFile:filePath atomically:YES];
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: filePath] == YES)
{
flag=1;
NSLog (@"File exists");
}
else
{
NSLog (@"File not found"); //always this happens in the log
}
}
沒有文件存在於filePath
。爲什麼? 有趣的部分是_recievedData.length
正在返回正確的文件大小。
永遠不會寫'if([filemgr fileExistsAtPath:filePath] == YES)'。 「真」條件可以產生除YES之外的許多值。 'fileExistsAtPath:'是_already_布爾值。寫'if([filemgr fileExistsAtPath:filePath])'。 – matt