Content-Type:image/png \r\n // 图片类型为png
Content-Transfer-Encoding: binary\r\n\r\n // 编码方式
// 这里是空一行,必不可少!!
... contents of boris.png ... // 图片数据部分
\r\n--Boundary+72D4CD655314C423--\r\n // 分隔符后面以"--"结尾,表明结束
__weak typeof(&*self)weakSelf = self;
UserInfo *userInfo = [UserInfo sharedUserInfo];
NSMutableDictionary *paramDic = [NSMutableDictionary dictionary];
paramDic[@"userId"] = userInfo.userId;
paramDic[@"elderId"] = [NSString stringWithFormat:@"%ld",(long)_model.ID];
paramDic[@"message"] = textView.text;
//以上是除图片外的参数的设置
//分界线的标识符
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
NSString * URLString = [NSString stringWithUTF8String:cHttpMethod[HTTP_COMMAND_ADDCHECKQUERY]];
NSString *urlStr = [NSString stringWithFormat:@"%@m=%@",URL_BASE,URLString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:10];
[request setHTTPMethod:@"POST"];
//分界线 --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
NSMutableArray *dataMuArr = [NSMutableArray array];
for (UIImage *image in _images) {
NSData* imageData = UIImageJPEGRepresentation(image, 0.1);
[dataMuArr addObject:imageData];
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dataMuArr];
// //要上传的图片
// UIImage *image=[_images firstObject];
// //得到图片的data
// NSData* data = UIImageJPEGRepresentation(image, 0.1);
//http body的字符串
NSMutableString *body=[[NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [paramDic allKeys];
//遍历keys
for(int i=0;i<[keys count];i++)
{
//得到当前key
NSString *key=[keys objectAtIndex:i];
//添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//添加字段名称,换2行
[body appendFormat:@"Content-Disposition: form-data; name="%@"\r\n\r\n",key];
//添加字段的值
[body appendFormat:@"%@\r\n",[paramDic objectForKey:key]];
}
//添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//声明pic字段,文件名为boris.png
[body appendFormat:@"Content-Disposition: form-data; name="files"; filename="boris.jpg"\r\n"];
//声明上传文件的格式
[body appendFormat:@"Content-Type: image/jpeg\r\n\r\n"];
//声明结束符:--AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[NSMutableData data];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
//将image的data加入
[myRequestData appendData:data];
//加入结束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[request setValue:[NSString stringWithFormat:@"%ld",(long) [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[request setHTTPBody:myRequestData];
//http method
//1.创建NSURLSession对象(单例)
NSURLSession *session = [NSURLSession sharedSession];
//2.创建NSURLSessionDataTask对象
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"error:%@",error);
}else{
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"result:%@",result);
}
}];
//3.发送请求(执行数据任务)
[task resume];