上传图片和数据到服务器 是最基本的需求了,有些学IOS的 可能对服务器怎么接收图片这块不太了解。 所以今天 我把服务器的代码 也拷过来了。。。 只会ASP.NET 。。 PHP 和J2EE 应该差不多 都是几句话的事
IOS端代码:
NSString * path = [[ NSBundle mainBundle ] pathForResource : @"iphone1-1-10" ofType : @"png" ];#pragma mark 使用ASIHttpRequest 上传图片和数据
ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.1/IOSUPLOAD/default.aspx"]];
[request addFile:path forKey:@"img"];
[request addPostValue:@"asihttp" forKey:@"name"];
[request setCompletionBlock:^{
NSLog(@"%@",request.responseString);
}];
[request setFailedBlock:^{
NSLog(@"asi error: %@",request.error.debugDescription);
}];
[request startAsynchronous];
#pragma mark 使用MKNetworkKit 上传图片和数据
MKNetworkEngine* engine = [[[MKNetworkEngine alloc]init]autorelease];
NSDictionary* postvalues = [NSDictionary dictionaryWithObjectsAndKeys:@"mknetwork",@"name",nil];
MKNetworkOperation* op = [engine operationWithURLString:@"http://192.168.0.1/IOSUPLOAD/default.aspx" params:postvalues httpMethod:@"POST"];
[op addFile:path forKey:@"img"];
[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
NSLog(@"%@",request.responseString);
}errorHandler:^(MKNetworkOperation *completedOperation,NSError *error) {
NSLog(@"mknetwork error : %@",error.debugDescription);
}];
[engine enqueueOperation: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# 大概也能看出来怎么获取数据了