上传图片和数据到服务器 是最基本的需求了,有些学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服务端的代码:
[csharp] www.2cto.com
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");
}
}