multipart接收多张图片_iOS开发 multipart 上传多张图片

这篇博客介绍了如何在iOS应用中使用multipart请求上传多张图片。通过创建一个包含参数和图片数据的请求体,然后利用NSMutableData组装请求数据,最后设置请求头并发送POST请求。代码示例展示了如何迭代图片数组并将其添加到请求体中。
摘要由CSDN通过智能技术生成

- (void)uploade:(NSDictionary *)dic pic:(NSArray *)picArray {

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];

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值