将一张IOS沙盒下的图片或是txt文件通过post请求上传服务器

首先对于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下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值