美文网首页移动开发iOS 开发 iOS Developer
通过表单多张图片同时上传 --iOS可乐

通过表单多张图片同时上传 --iOS可乐

作者: 996打工仔 | 来源:发表于2016-06-12 17:15 被阅读1004次

    上篇写到一张上传的,但不实用,毕竟发表动态基本都是多张上传,从网上也找好多大神写的,现在就简单总结一下。

    请求方式post

    -(NSString *)PostImagesToServer:(NSString *) strUrl dicPostParams:(NSMutableDictionary *)params dicImages:(NSMutableArray *) dicImages{

    NSString * res;

    //分界线的标识符

    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";

    //根据url初始化request

    NSURL *url = [NSURL URLWithString:strUrl];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    //分界线 --AaB03x

    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];

    //结束符 AaB03x--

    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];

    //要上传的图片

    UIImage *image;

    //http body的字符串

    NSMutableString *body=[[NSMutableString alloc]init];

    //参数的集合的所有key的集合

    NSArray *keys= [params allKeys];

    //遍历keys

    for(int i=0;i<[keys count];i++) {

    //得到当前key

    NSString *key=[keys objectAtIndex:i];

    //如果key不是pic,说明value是字符类型,比如name:Boris

    //if(![key isEqualToString:@"pic"]) {

    //添加分界线,换行

    [body appendFormat:@"%@\r\n",MPboundary];

    //添加字段名称,换2行

    [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];

    //添加字段的值

    [body appendFormat:@"%@\r\n",[params objectForKey:key]];

    }

    //声明myRequestData,用来放入http body

    NSMutableData *myRequestData=[NSMutableData data];

    //将body字符串转化为UTF8格式的二进制

    [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];

    //循环加入上传图片

    for(int i = 0; i< [dicImages count] ; i++){

    //要上传的图片

    image = [dicImages objectAtIndex:i];

    //得到图片的data

    NSData* data =  UIImageJPEGRepresentation(image, 0.0);

    NSMutableString *imgbody = [[NSMutableString alloc] init];

    //此处循环添加图片文件

    ////添加分界线,换行

    [imgbody appendFormat:@"%@\r\n",MPboundary];

    [imgbody appendFormat:@"Content-Disposition: form-data; name=\"content2[]\"; filename=\"%@.jpg\"\r\n",[self.imageArray objectAtIndex:i]];

    //声明上传文件的格式

    //[imgbody appendFormat:@"Content-Type: application/octet-stream; charset=utf-8\r\n\r\n"];

    [imgbody appendFormat:@"Content-Type: image/jpeg; charset=utf-8\r\n\r\n"];

    NSLog(@"上传的图片:%d  %@", i, [dicImages objectAtIndex:i]);

    //将body字符串转化为UTF8格式的二进制

    [myRequestData appendData:[imgbody dataUsingEncoding:NSUTF8StringEncoding]];

    //将image的data加入

    [myRequestData appendData:data];

    [myRequestData appendData:[ @"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    }

    //声明结束符:--AaB03x--

    NSString *end=[[NSString alloc]initWithFormat:@"%@\r\n",endMPboundary];

    //加入结束符--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:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];

    //设置http body

    [request setHTTPBody:myRequestData];

    //http method

    [request setHTTPMethod:@"POST"];

    //设置接受response的data

    NSData *mResponseData;

    NSError *err = nil;

    mResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&err];

    if(mResponseData == nil){

    NSLog(@"err code : %@", [err localizedDescription]);

    }

    res = [[NSString alloc] initWithData:mResponseData encoding:NSUTF8StringEncoding];

    NSLog(@"服务器返回:%@", res);

    return res;

    }

    params 表单数据

    dicImages 图片数组或字典,我的是数组,所以改了。

    相关文章

      网友评论

        本文标题:通过表单多张图片同时上传 --iOS可乐

        本文链接:https://www.haomeiwen.com/subject/mzgedttx.html