iOS Asihttp上传文件(图片等)以及服务端的代码(c#,.net Web api2)
2014-11-26 16:21
465 查看
1,客户端的代码
NSString *api = @"http://192.168.20.189:6900/bk/api/upload"; ASIFormDataRequest *req = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:api]]; [req setRequestMethod:@"POST"]; // NSString *imgPath = @"/Users/duanhai/Desktop/test.png"; NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@".png"]; // NSData *image = [[NSData alloc] initWithContentsOfFile:path]; // [req addData:image withFileName:@"test.png" andContentType:@"image/jpeg" forKey:@"image"]; [req setFile:path forKey:@"whatever"]; [req setCompletionBlock:^{ NSLog(@"xxxx is %@ and err is %@",req.responseString,req.error); }]; [req setFailedBlock:^{ NSLog(@"err code is %@",[req.error localizedDescription]); }]; [req startAsynchronous];2,服务端代码
[RoutePrefix("api/upload")]public class UploadController : ApiController{public HttpResponseMessage Post(){HttpResponseMessage result = null;var httpRequest = HttpContext.Current.Request;if (httpRequest.Files.Count > 0){var docfiles = new List<string>();foreach (string file in httpRequest.Files){var postedFile = httpRequest.Files[file];// var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName);var filePath = "C:/Users/Tony_mac/Desktop/upload/"+postedFile.FileName;postedFile.SaveAs(filePath);docfiles.Add(filePath);}result = Request.CreateResponse(HttpStatusCode.Created, docfiles);}else{result = Request.CreateResponse(HttpStatusCode.BadRequest);}return result;}}服务端我是建了一个虚拟目录,故api地址中有个bk。
相关文章推荐
- iOS 多张图片上传demo(用ASIHTTPRequest 框架实现)
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- iOS开发-使用NSURLSession实现文件断点下载,文件离线续传以及图片上传
- Android实现文件,图片上传,以及服务端接收相关。
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- iOS图片上传服务器(ASIHTTPRequest,SpringMVC)
- iOS 用ASIHTTPRequest框架 上传多张图片
- iOS开发-使用AFN实现GET/POST请求,以及文件下载/文件(图片)上传
- ueditor1.4.3配置过程(包含单独上传文件以及图片的使用),ueditor1.4.3上传配置(转 http://www.bkjia.com/webzh/1001016.html)
- c#上传图片到SQL中,以及读取的代码
- iOS ASIHTTPRequest上传图片的一个问题
- IOS 使用ASIHttpRequest 或 MKNetworkKit 上传图片到ASP.NET
- 转:ios的图片文件上传代码
- IOS 使用ASIHttpRequest 或 MKNetworkKit 上传图片到ASP.NET
- iOS用ASIHTTPRequest框架实现多张图片上传
- iOS通过http post上传图片, 文件等
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- IOS 使用ASIHttpRequest 或 MKNetworkKit 上传图片到ASP.NET