读书人

IOS 使用ASIHttpRequest 或 MKNetwork

发布时间: 2014-06-17 16:31:44 作者: rapoo

IOS 使用ASIHttpRequest 或 MKNetworkKit 上传图片到ASP.NET

上传图片和数据到服务器 是最基本的需求了,有些学IOS的 可能对服务器怎么接收图片这块不太了解。 所以今天 我把服务器的代码 也拷过来了。。。 只会ASP.NET 。。 PHP 和J2EE 应该差不多 都是几句话的事

IOS端代码:

NSString* path = [[NSBundlemainBundle]pathForResource:@"iphone1-1-10"ofType:@"png"];

#pragma mark 使用ASIHttpRequest 上传图片和数据

ASIFormDataRequest* request = [ASIFormDataRequestrequestWithURL:[NSURLURLWithString:@"http://192.168.0.1/IOSUPLOAD/default.aspx"]];

[requestaddFile:pathforKey:@"img"];

[requestaddPostValue:@"asihttp"forKey:@"name"];

[request setCompletionBlock:^{

NSLog(@"%@",request.responseString);

}];

[requestsetFailedBlock:^{

NSLog(@"asi error: %@",request.error.debugDescription);

}];

[request startAsynchronous];

#pragma mark 使用MKNetworkKit 上传图片和数据

MKNetworkEngine* engine = [[[MKNetworkEnginealloc]init]autorelease];

NSDictionary* postvalues = [NSDictionarydictionaryWithObjectsAndKeys:@"mknetwork",@"name",nil];

MKNetworkOperation* op = [engineoperationWithURLString:@"http://192.168.0.1/IOSUPLOAD/default.aspx"params:postvalueshttpMethod:@"POST"];

[opaddFile:pathforKey:@"img"];

[opaddCompletionHandler:^(MKNetworkOperation *completedOperation) {

NSLog(@"%@",request.responseString);

}errorHandler:^(MKNetworkOperation *completedOperation,NSError *error) {

NSLog(@"mknetwork error : %@",error.debugDescription);

}];

[engineenqueueOperation:op];

//看上去就是跟示例的差不多

ASP.NET服务端的代码:

protected void Page_Load(object sender, EventArgs e){    if (Request.Files.Count == 0)    {        Response.Write("none file");    }    else    {        HttpPostedFile file = Request.Files["img"];        String filename = Request.Form["name"];        file.SaveAs(MapPath("~/"+filename+".png"));                Response.Write("ok");    }}

N 简单吧 就算你不会C# 大概也能看出来怎么获取数据了

打包下载: http://download.csdn.net/detail/li6185377/5027605

读书人网 >操作系统

热点推荐