IOS学习之 网络编程(6)--文件上传
2015-11-30 12:27
513 查看
转载自 http://www.cnblogs.com/wendingding/p/3949966.html
说明:文件上传使用的时POST请求,通常把要上传的数据保存在请求体中。本文介绍如何不借助第三方框架实现iOS开发中得文件上传。 由于过程较为复杂,因此本文只贴出部分关键代码。主控制器的关键代码:YYViewController.m
部分文件的MIMEType
说明:文件上传使用的时POST请求,通常把要上传的数据保存在请求体中。本文介绍如何不借助第三方框架实现iOS开发中得文件上传。 由于过程较为复杂,因此本文只贴出部分关键代码。主控制器的关键代码:YYViewController.m
#import "YYViewController.h" #define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding] @interface YYViewController () @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params { // 文件上传 NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/YYServer/upload"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; // 设置请求体 NSMutableData *body = [NSMutableData data]; /***************文件参数***************/ // 参数开始的标志 [body appendData:YYEncode(@"--YY\r\n")]; // name : 指定参数名(必须跟服务器端保持一致) // filename : 文件名 NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename]; [body appendData:YYEncode(disposition)]; NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType]; [body appendData:YYEncode(type)]; [body appendData:YYEncode(@"\r\n")]; [body appendData:data]; [body appendData:YYEncode(@"\r\n")]; /***************普通参数***************/ [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { // 参数开始的标志 [body appendData:YYEncode(@"--YY\r\n")]; NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key]; [body appendData:YYEncode(disposition)]; [body appendData:YYEncode(@"\r\n")]; [body appendData:YYEncode(obj)]; [body appendData:YYEncode(@"\r\n")]; }]; /***************参数结束***************/ // YY--\r\n [body appendData:YYEncode(@"--YY--\r\n")]; request.HTTPBody = body; // 设置请求头 // 请求体的长度 [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"]; // 声明这个POST请求是个文件上传 [request setValue:@"multipart/form-data; boundary=YY" forHTTPHeaderField:@"Content-Type"]; // 发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"%@", dict); } else { NSLog(@"上传失败"); } }]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // Socket 实现断点上传 //apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType // UIImage *image = [UIImage imageNamed:@"test"]; // NSData *filedata = UIImagePNGRepresentation(image); // [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}]; // 给本地文件发送一个请求 NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil]; NSURLRequest *request = [NSURLRequest requestWithURL:fileurl]; NSURLResponse *repsonse = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil]; // 得到mimeType NSLog(@"%@", repsonse.MIMEType); [self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{ @"username" : @"999", @"type" : @"XML"}]; } @end补充说明:文件上传请求数据格式
部分文件的MIMEType
相关文章推荐
- android wifi 无线调试
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 拨号网络的简单知识
- 菜鸟必看网络名词
- 通晓网络测试常用命令
- 网络路由技术及运用2
- IP网络路由技术
- 网络防火墙的设置技巧
- 网络管理之IP地址篇
- 网络路由技术及运用3
- 网络路由技术及运用4
- 网络路由技术及运用1
- 无线局域网络简介一