NSString *hyphens = @"--";
NSString *boundary = @"*****";
NSString *end = @"\r\n";
NSMutableData *myRequestData1 = [NSMutableData data];
//参数的集合的所有key的集合
NSArray *keys= [dic allKeys];
//添加其他参数
for(int i = 0;i < [keys count];i ++)
{
NSMutableString *body = [[NSMutableString alloc]init];
[body appendString:hyphens];
[body appendString:boundary];
[body appendString:end];
//得到当前key
NSString *key = [keys objectAtIndex:i];
//添加字段名称
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"%@%@",key,end,end];
//添加字段的值
[body appendFormat:@"%@",[dic objectForKey:key]];
[body appendString:end];
[myRequestData1 appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"添加字段的值==%@",[dic objectForKey:key]);
}
//遍历数组,添加多张图片
for (int i = 0; i < picArray.count; i ++) {
//所有字段的拼接都不能缺少,要保证格式正确
[myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[boundary dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableString *fileTitle = [[NSMutableString alloc]init];
//要上传的文件名和key,服务器端用file接收
[fileTitle appendFormat:@"Content-Disposition: form-data; name=\"img%d\"; filename=\"pic_%d\"%@",i,i,end];
[fileTitle appendString:[NSString stringWithFormat:@"Content-Type:image/jpeg%@%@",end,end]];
[myRequestData1 appendData:[fileTitle dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:picArray[i]];
[myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
}
[myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[boundary dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[hyphens dataUsingEncoding:NSUTF8StringEncoding]];
[myRequestData1 appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//根据url初始化request
NSURL *URL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@addAppCollecting",SERVER_ADDRESS]];
request = [[NSMutableURLRequest alloc] initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
//http method
[request setHTTPMethod:@"POST"];
//设置HTTPHeader中Content-Type的值
NSString *content = [[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",boundary];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData1 length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//设置http body
[request setHTTPBody:myRequestData1];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_connection start];
}
#pragma mark - connection delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"reveive Response:\n%@",response);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (!_reveivedData) {
_reveivedData = [[NSMutableData alloc]init];
}
[_reveivedData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSError *err;
NSDictionary *note = [NSJSONSerialization JSONObjectWithData:_reveivedData options:NSJSONReadingMutableContainers error:&err];
if ([note[@"success"] integerValue] == 1) {
[ICAPI hudWithText:@"添加成功!" atView:self.view];
self.custom = nil;
self.addressInfo = nil;
self.picArray = nil;
self.infoArray = nil;
self.signListArray = nil;
customDic = [NSMutableDictionary dictionary];
customDic[@"address"] = @"";
customDic[@"customName"] = @"";
[self.tableView reloadData];
} else {
[ICAPI hudWithText:netErrorMessage atView:self.view];
}
}