首先对于txt文件
我们先取得沙盒的路径
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
NSString* documentDictionary = [paths objectAtIndex:0];
创建一个名字为test的文件夹:
NSFileManager* fileManager = [NSFileManager defaultManager];
NSString* testDictionary = [documentDictionary stringByAppendingPathComponent:@"test"];
[fileManager createDirectoryAtPath:testDictionary withIntermediateDirectories:YES attributes:nil error:nil];
然后 创建11.txt写入内容,然后把内容读出来看看内容写入的是否正确
NSString* testFileName = [testDictionary stringByAppendingPathComponent:@"11.txt"];
NSString* str = @"file content";
[fileManager removeItemAtPath:testFileName error:nil];
[fileManager createFileAtPath:testFileName contents:[str dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSData *reader = [NSData dataWithContentsOfFile:testFileName];
NSString* content = [[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding];
NSLog(@"content=%@",content);
对于项目自带的图片Placeholder.png我们把这个图片存到沙盒中,命名为11.png
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"Placeholder" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
NSString* testFileName = [testDictionary stringByAppendingPathComponent:@"11.png"];
[UIImagePNGRepresentation(appleImage) writeToFile:testFileName atomically:YES];
BOOL isExsit = [fileManager fileExistsAtPath:testFileName];
发送post请求给服务器:
-(void) updateFileToServer:(NSString*) path{
NSString *url = @"http://2.novelread.sinaapp.com/framework-sae/index.php?c=main&a=updateTxt";
HTTPFileUpload* httpFileUpload = [[HTTPFileUpload alloc] init];
httpFileUpload.delegate = self;
[httpFileUpload postWithUri:url filePath:path];
[httpFileUpload release];
}
关于发http的请求实现如下:
- (void)postWithUri:(NSString *)uri filePath:(NSString *)filepath
{
NSMutableData *postData = [[[NSMutableData alloc] init] autorelease];
[postData appendData:[NSData dataWithContentsOfFile:filepath]];
// Post data.
NSMutableURLRequest *request;
request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:uri]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30] autorelease];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
// [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
resultData_ = [[NSMutableData alloc] init];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *result = [[NSString alloc] initWithData:resultData_ encoding:NSUTF8StringEncoding];
NSLog(@"The result is %@", result);
if ([delegate_ respondsToSelector:@selector(httpFileUploadDidFinishLoading:result:)]) {
[delegate_ httpFileUploadDidFinishLoading:connection result:result];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[resultData_ release], resultData_ = nil;
[result release];
[connection cancel];
}
代码可以在 http://download.csdn.net/detail/baidu_nod/7518699下载