#pragma mark - uploadFile
- (void)uploadFile:(NSDictionary *)dic {
   
    NSURL *URL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@addVisitRecord",SERVER_ADDRESS]];
    request = [[NSMutableURLRequest alloc] initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    NSString *boundary = @"wfWiEWrgEFA9A78512weF7106A";
    request.HTTPMethod = @"POST";
    request.allHTTPHeaderFields = @{@"Content-Type":[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]};
    //multipart/form-data格式按照构建上传数据
    NSMutableData *postData = [[NSMutableData alloc] init];
    for (NSString *key in dic) {
        NSString *pair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n",boundary,key];
        [postData appendData:[pair dataUsingEncoding:NSUTF8StringEncoding]];
       
        id value = [dic objectForKey:key];
        if ([value isKindOfClass:[NSString class]]) {
            [postData appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
        } else if ([value isKindOfClass:[NSData class]]){
            [postData appendData:value];
        }
        [postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    //文件部分
    NSString *filename = [filePath lastPathComponent];
    NSString *contentType = AFContentTypeForPathExtension([filePath pathExtension]);
   
    NSString *filePair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\";Content-Type=%@\r\n\r\n",boundary,@"img",filename,contentType];
    [postData appendData:[filePair dataUsingEncoding:NSUTF8StringEncoding]];
   
    [postData appendData:self.imageSecondFile]; //加入文件的数据
   
    [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    request.HTTPBody = postData;
    [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"];
    _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{
    [[CRMDatahandle shareDataHandle] saveLocalData:@"REFRESH_PLAY" value:@"YES"];
    NSError *err;
    NSDictionary *note = [NSJSONSerialization JSONObjectWithData:_reveivedData  options:NSJSONReadingMutableContainers error:&err];
    if ([note[@"success"] integerValue] == 1) {
        [CRMDatahandle hudWithText:@"添加成功!" atView:self.view];
        [[IQKeyboardManager sharedManager] setEnable:NO];
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(succesPaly) userInfo:nil repeats:NO];
    }
  

}

iOS Multipart上传单张图片的更多相关文章

  1. Uploadify在MVC中使用方法案例(一个视图多次上传单张图片)

    Controller 中代码和 上一节文章(http://www.cnblogs.com/yechangzhong-826217795/p/3785842.html )一样 视图中代码如下: < ...

  2. Uploadify在MVC中使用方法案例(上传单张图片)

    在View视图中: <link href="/Scripts/uploadify-v3.2.1/uploadify.css" rel="stylesheet&quo ...

  3. 封装组件el-upload通过v-model (一): 上传单张图片

    ElementUI 中的el-upload 上传图片 我进行了二次封装.(默认大家都是有一定的vue基础的,细节就不过多的讲了) 在项目中我们主要拿到图片或者其他的一些参数 ,我这里是上传后返回的Gu ...

  4. 微信小程序云开发-云存储-上传单张照片到云存储并显示到页面上

    一.wxml文件 页面上写上传图片的按钮,按钮绑定chooseImg. <button bindtap="chooseImg" type="primary" ...

  5. struts2学习笔记--上传单个和批量文件示例

    struts2提供了对上传文件的支持,将上传后的文件封装为java.io.File对象,开发者只需要在Action中定义一个File类型的变量,然后直接使用该变量,将它复制到目的目录即可. 单个文件上 ...

  6. 微信JSSDK多图片上传并且解决IOS系统上传一直加载的问题

    微信多图片上传必须挨个上传,也就是不能并行,得串行: 那么我们可以定义一个如下所示的上传函数: var serverIds = []; function uploadImages(localImage ...

  7. java接受安卓及ios App上传的图片,并保存到阿里OSS

    做后台的时候,写了两个方法,分别用来获取安卓和苹果IOS端上传的头像,保存到阿里云OSS图片存储服务器上.(SMM框架) 安卓及H5版本: /** * 上传用户头像 */ @RequestMappin ...

  8. HTML实现点击时的阴影(:active)(已解决iOS微信上无法使用)

    一般是用这个委类:active 参考:http://www.w3school.com.cn/cssref/selector_active.asp 但是业界上实现的效果中,今日头条的手机端网页却不是这样 ...

  9. 如何在iOS地图上高效的显示大量数据

    2016-01-13 / 23:02:13 刚才在微信上看到这篇由cocoachina翻译小组成员翻译的文章,觉得还是挺值得参考的,因此转载至此,原文请移步:http://robots.thought ...

随机推荐

  1. windows 装 Crypto.Cipher

    python 2.7 先去官网下载,https://www.dlitz.net/software/pycrypto/ 回来装这个,http://download.csdn.net/download/d ...

  2. noip2006解题报告

    T1.能量项链 给出一串数字(其实是个环也就是可以旋转).n个数组成n颗珠子,形如: 1 2 3 4 表示的珠子是(1,2)(2,3)(3,4)(4,1) 定义珠子的聚合:如前两颗聚合放出能量为1*2 ...

  3. cs11_c++_lab2

    Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...

  4. ARCGIS对谷歌影像进行投影转换

    相信有不少同学会有这样的困扰,通过软件下载的谷歌遥感影像,直接用ARCGIS等专业软件打开之后发现,遥感影像有拉伸的情况,这是什么原因呢.那是因为,通过软件下载下来的遥感影像的投影信息包含的是经纬度信 ...

  5. Selenium+Python+Eclipse网页自动化集成环境配置(附简单的测试程序)

    最近公司在给我们培训,主要是网页自动化测试的,现在的工作每天都是测APP,刚刚入门,不过,当我看了别人写的bug之后,就觉得不会觉得能够发现bug多么多么的厉害了. 前两周的时间一直在搭建自动化测试的 ...

  6. XidianOJ 1112 Too stupid

    题目描述 某天 light由于太富而且太帅遭到了歹徒的袭击,现在他遇到了n个歹徒,准备对light施行不法行为,虽然light身体强壮,但是毕竟只有一个人肯定打不过那么多歹徒,但是高智商的light觉 ...

  7. Python 2.7_Second_try_爬取阳光电影网_获取电影下载地址并写入文件 20161207

    1.昨天文章http://www.cnblogs.com/Mr-Cxy/p/6139705.html 是获取电影网站主菜单 然后获取每个菜单下的电影url  2.今天是对电影url 进行再次解析获取下 ...

  8. linux c libcurl的简单使用(转)

    curl是Linux下一个非常著名的下载库,通过这个库,可以很简单的实现文件的下载等操作.看一个简单的例子: #include <curl/curl.h> #include <std ...

  9. Swift3.0基础语法学习<四>

    协议和扩展: // // ViewController4.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/16. // Copyright ...

  10. 使用Debussy+ModelSim快速查看前仿真波形

    sim.do文件 quit -sim set PATH1 D:/Program/modelsim/vivado_lib set PATH2 D:/Program/Vivado/Vivado/2014. ...