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

 

iOS开发 multipart 上传多张图片的更多相关文章

  1. IOS开发-图片上传

    目前IOS端开发,图片上传到服务器分为两种,一种是直接上到服务器,一种是借助第三方储存(减少服务器压力). 一.直接上传到服务器 /** * 代码演示 */ //*******UIImagePNGRe ...

  2. iOS 一次上传多张图片, 并返回进度值

    我封装的使用AFNetworking上传多图的一个方法.带进度条回调,希望对你用帮助,我app中亲测可用,不知道你的服务器是如何接收数据的: /** * 上传带图片的内容,允许多张图片上传(URL)P ...

  3. iOS开发App上传的三大步骤

    上传流程 1.itunse connect中->“我的App”中新建创建应用,填写相关的信息 a.项目名称(多创建几个),避免重名 b.想好应用的类型 c.应用截图(5.5,4.7,4,3.5寸 ...

  4. IOS开发之—— 上传头像的使用

    static NSString *const uploadSuccess = @"更改头像成功"; @interface DMAccountInformationViewContr ...

  5. iOS开发--CornerStone上传静态库(.a文件)

    首先打开软件左上角 CornerStone-Preferences-SubVersion 第一个地方把对号去掉,第二个地方把.a那个删除,然后save. 然后把你的.a文件放到本地的相应文件夹下, 但 ...

  6. iOS -- 上传多张图片 后台(PHP)代码和上传一张的一样

    // 上传多张图片 - (void)send { // 设置初始记录量为0 self.count = 0; self.upcount = 0; // 设置初始值为NO self.isUploadPic ...

  7. POST请求上传多张图片并携带参数

    POST请求上传多张图片并携带参数 在iOS中,用POST请求携带参数上传图片是非常恶心的事情,HTTPBody部分完全需要我们自己来配置,这个HTTPBody分为3个部分,头部分可以携带参数,中间部 ...

  8. 【iOS 使用github上传代码】详解

    [iOS 使用github上传代码]详解 一.github创建新工程 二.直接添加文件 三.通过https 和 SSH 操作两种方式上传工程 3.1https 和 SSH 的区别: 3.1.1.前者可 ...

  9. 微信JSSDK上传多张图片

    之前是使用for循环实现的,但是安卓手机没有问题,苹果手机只能上传最后一张图片. 好在有高手在前面趟路,实用的循环调用.苹果是没有,安卓不清楚.以下内容转自:http://leo108.com/pid ...

随机推荐

  1. ubuntu NTP server 搭建

    ubuntu server ntp时间同步服务器安装及使用一.服务端1 apt-get install ntp 2 安装后默认启动服务,如果没有启动,启动之. /etc/init.d/ntp star ...

  2. php 向asmx发送请求 || php 发送xml请求, 以及处理返回的xml结果

    var $live_url = 'https://processing.ukash.com/RPPGateway/process.asmx'; $source = array( 'SecurityTo ...

  3. Android软件更新安装。

    app的开发有一个问题是避免不了的,那就是软件的升级维护. 这里我在查过一些资料和写了一个升级帮助类.使用很方便.直接导入就可以了. ( VersionBean.class为更新地址返回的数据对象,我 ...

  4. mysql高可用框架-MHA

    MHA高可用架构 用一个管理节点监控后端数据库主库可用性 提供VIP漂移接口,不提供具体方法 提供补全从库日志的脚本   MHA 监控主库,提供自动主从切换: 提供VIP漂移接口 提供补全从库日志的脚 ...

  5. 根据某个文件或文件夹自制rpm包

    首先安装打包软件# yum install rpmbuild  rpmdevtools -y 生成打包路径1.使用rpmdev-setuptree ,会自动在用户家目录生成一个rpmbuild的文件夹 ...

  6. 一个最简单的登录页面测试case

    具体需求: 有一个登陆页面, (假如上面有2个textbox, 一个提交按钮. 请针对这个页面设计30个以上的testcase.) 此题的考察目的:面试者是否熟悉各种测试方法,是否有丰富的Web测试经 ...

  7. 《深入理解Nginx》阅读与实践(四):简单的HTTP过滤模块

    一.Nginx的HTTP过滤模块特征 一个请求可以被任意个HTTP模块处理: 在普通HTTP模块处理请求完毕并调用ngx_http_send_header()发送HTTP头部或调用ngx_http_o ...

  8. 企业内部从零开始安装docker hadoop 提纲

    下载apache 项目  http://mirror.bit.edu.cn/apache/ 下载 centos 7 安装 盘 iso 大约7G 安装 centos7 copy 光盘盘中的 packag ...

  9. .NET/C#/Oracle数据库操作类

    public static class OracleHelper { //数据库连接字符串 private readonly static string connstr = Configuration ...

  10. 51单片机ALE引脚的控制(摘录)

    ALE/PROG: 当访问外部存储器时,地址锁存允许的输出电平用于锁存地址的地位字节. 在FLASH编程期间,此引脚用于输入编程脉冲. 在平时,ALE端以不变的频率周期输出正脉冲信号,此频率为振荡器频 ...