星空网 > 软件开发 > 操作系统

文件的上传(转)

iOS开发网络篇—文件的上传

说明:文件上传使用的时POST请求,通常把要上传的数据保存在请求体中。本文介绍如何不借助第三方框架实现iOS开发中得文件上传。

  由于过程较为复杂,因此本文只贴出部分关键代码。

主控制器的关键代码:

YYViewController.m

文件的上传(转)images/loading.gif' data-original="http://common.cnblogs.com/images/copycode.gif" />
 1 #import "YYViewController.h" 2 3 #define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding] 4 5 @interface YYViewController () 6 7 @end 8 9 @implementation YYViewController10 11 - (void)viewDidLoad12 {13   [super viewDidLoad];14   // Do any additional setup after loading the view, typically from a nib.15 }16 17 - (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params18 {19   // 文件上传20   NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/YYServer/upload"];21   NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];22   request.HTTPMethod = @"POST";23   24   // 设置请求体25   NSMutableData *body = [NSMutableData data];26   27   /***************文件参数***************/28   // 参数开始的标志29   [body appendData:YYEncode(@"--YY\r\n")];30   // name : 指定参数名(必须跟服务器端保持一致)31   // filename : 文件名32   NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename];33   [body appendData:YYEncode(disposition)];34   NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType];35   [body appendData:YYEncode(type)];36   37   [body appendData:YYEncode(@"\r\n")];38   [body appendData:data];39   [body appendData:YYEncode(@"\r\n")];40   41   /***************普通参数***************/42   [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {43     // 参数开始的标志44     [body appendData:YYEncode(@"--YY\r\n")];45     NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key];46     [body appendData:YYEncode(disposition)];47 48     [body appendData:YYEncode(@"\r\n")];49     [body appendData:YYEncode(obj)];50     [body appendData:YYEncode(@"\r\n")];51   }];52   53   /***************参数结束***************/54   // YY--\r\n55   [body appendData:YYEncode(@"--YY--\r\n")];56   request.HTTPBody = body;57   58   // 设置请求头59   // 请求体的长度60   [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"];61   // 声明这个POST请求是个文件上传62   [request setValue:@"multipart/form-data; boundary=YY" forHTTPHeaderField:@"Content-Type"];63   64   // 发送请求65   [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {66     if (data) {67       NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];68       NSLog(@"%@", dict);69     } else {70       NSLog(@"上传失败");71     }72   }];73 }74 75 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event76 {77   // Socket 实现断点上传78   79   //apache-tomcat-6.0.41/conf/web.文件的上传(转)

补充说明:

文件上传请求数据格式

文件的上传(转)

部分文件的MIMEType

文件的上传(转)

 

如何获取MIMEType?

完全可以添加一个NSString的类别,直接获取当前字符串的MIMEType

- (NSString *)MIMEType {    NSURLResponse *response = nil;  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:self]];    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];  return response.MIMEType;}

 




原标题:文件的上传(转)

关键词:上传

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

产品变体:https://www.goluckyvip.com/tag/7279.html
境外预约开户:https://www.goluckyvip.com/tag/72790.html
境外开户预约:https://www.goluckyvip.com/tag/72791.html
如何给境外账户汇款:https://www.goluckyvip.com/tag/72792.html
怎么在海外开户:https://www.goluckyvip.com/tag/72793.html
个人如何在境外开户:https://www.goluckyvip.com/tag/72796.html
大福地快捷酒店预订 大福酒店怎么走:https://www.vstour.cn/a/365187.html
三亚有哪些酒店值得入住?:https://www.vstour.cn/a/366173.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流