iOS上传文件到服务器(ASIHttpRequest)
2016-03-16 15:28
756 查看
本人工作使用,测试通过无误
ASIHttpRequest(下载地址):http://www.oschina.net/p/asihttprequest
导入头文件
#import "ASIFormDataRequest.h"
//上传数据
// ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://sview.sv3d.cn/quickpass/upload"]];
//http://10.0.88.214:8080/SViewWeb/quickpass/upload测试用
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://10.0.88.214:8080/SViewWeb/quickpass/upload"]];
[request setFile:self.fileRoute forKey:@"modelfile"];//上传文件
[request setFile:self.imagePath forKey:@"upimage"];
[request setUploadProgressDelegate:self];//设置上传进度代理
request.showAccurateProgress = YES;
request.delegate=self;
[request setPostValue:self.fileTextView.text forKey:@"description"];
[request setPostValue:self.nameField.text forKey:@"name"];
[request setDidFailSelector:@selector(requestDidFailed:)];//上传失败
[request setDidFinishSelector:@selector(requestDidSuccess:)];//上传成功
[request startAsynchronous];
}
//执行成功
- (void)requestDidSuccess:(ASIFormDataRequest *)request
{
//获取头文件
NSDictionary *headers = [request responseHeaders];
NSString *method = [request.userInfo objectForKey:@"Method"];
//获取http协议执行代码
NSLog(@"Code:%d",[request responseStatusCode]);
if ([self respondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)])
{
//执行委托操作
(架构设计
自选)
[self OARequestSuccessed:method withResponse:[request responseString] WithData:[request responseData] withHeaders:headers];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//执行失败
- (void)requestDidFailed:(ASIFormDataRequest *)request{
//获取的用户自定义内容
NSString *method = [request.userInfo objectForKey:@"Method"];
//获取错误数据
NSError *error = [request error];
if ([self respondsToSelector:@selector(OARequestFailed:withError:)])
{
//执行委托
将错误数据传其他方式(架构设计 自选)
[self OARequestFailed:method withError:error];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//上传执行成功函数
- (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers
{
self.responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSRange range3 = [self.responseStr rangeOfString:@","];
// self.uuid = [self.responseStr substringFromIndex:range3.location+1];
// NSLog(@"%@",self.uuid);
//服务返回post后的数据,返回OK+uuid,根据需要得到相应的字符串
NSLog(@"response:\n%@",self.responseStr);
// [self createProgressView];
// [self reSaveHistoryNotes]; 上传历史记录,数据持久化存储
}
//上传执行失败函数
- (void)OARequestFailed:(NSString *)method withError:(NSError *)error
{
NSLog(@"Error:%@",error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败,
请稍后重试." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
[alert show];
}
ASIHttpRequest(下载地址):http://www.oschina.net/p/asihttprequest
导入头文件
#import "ASIFormDataRequest.h"
//上传数据
// ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://sview.sv3d.cn/quickpass/upload"]];
//http://10.0.88.214:8080/SViewWeb/quickpass/upload测试用
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://10.0.88.214:8080/SViewWeb/quickpass/upload"]];
[request setFile:self.fileRoute forKey:@"modelfile"];//上传文件
[request setFile:self.imagePath forKey:@"upimage"];
[request setUploadProgressDelegate:self];//设置上传进度代理
request.showAccurateProgress = YES;
request.delegate=self;
[request setPostValue:self.fileTextView.text forKey:@"description"];
[request setPostValue:self.nameField.text forKey:@"name"];
[request setDidFailSelector:@selector(requestDidFailed:)];//上传失败
[request setDidFinishSelector:@selector(requestDidSuccess:)];//上传成功
[request startAsynchronous];
}
//执行成功
- (void)requestDidSuccess:(ASIFormDataRequest *)request
{
//获取头文件
NSDictionary *headers = [request responseHeaders];
NSString *method = [request.userInfo objectForKey:@"Method"];
//获取http协议执行代码
NSLog(@"Code:%d",[request responseStatusCode]);
if ([self respondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)])
{
//执行委托操作
(架构设计
自选)
[self OARequestSuccessed:method withResponse:[request responseString] WithData:[request responseData] withHeaders:headers];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//执行失败
- (void)requestDidFailed:(ASIFormDataRequest *)request{
//获取的用户自定义内容
NSString *method = [request.userInfo objectForKey:@"Method"];
//获取错误数据
NSError *error = [request error];
if ([self respondsToSelector:@selector(OARequestFailed:withError:)])
{
//执行委托
将错误数据传其他方式(架构设计 自选)
[self OARequestFailed:method withError:error];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//上传执行成功函数
- (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers
{
self.responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSRange range3 = [self.responseStr rangeOfString:@","];
// self.uuid = [self.responseStr substringFromIndex:range3.location+1];
// NSLog(@"%@",self.uuid);
//服务返回post后的数据,返回OK+uuid,根据需要得到相应的字符串
NSLog(@"response:\n%@",self.responseStr);
// [self createProgressView];
// [self reSaveHistoryNotes]; 上传历史记录,数据持久化存储
}
//上传执行失败函数
- (void)OARequestFailed:(NSString *)method withError:(NSError *)error
{
NSLog(@"Error:%@",error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败,
请稍后重试." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
[alert show];
}
相关文章推荐
- afnetworking 和 asihttprequest 强制修改http连接的host地址
- IOS 学习笔记 —— ASIHTTPRequest 之 发送异步请求
- ASIHTTPRequest用户登陆:重复用户登陆问题解决
- ASIHTTPRequest
- ASIHttpRequest下载请求
- 简介
- libxml/HTMLparser.h file not found 解决方法
- ASIHTTPRequest中HTTP请求解释
- ASIHTTPRequest详解
- ASIHTTPRequest类库简介和使用说明
- iOS ASIHTTPRequest详解
- ASIHTTPRequest第三方网络请求详解
- ASIHTTPRequest
- 对比iOS网络组件:AFNetworking VS ASIHTTPRequest
- IOS部署ASIHTTPRequest框架
- ASIHTTPRequest
- ASIHttpRequest "_UTTypeCopyPreferredTagWithClass", referenced from:
- ASIHTTPRequest
- ASIHttpRequest的使用
- MKNetworkKit 的使用