#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. Qt 环境下的activex控件编程-------1

    本人第一次接触这种activeX控件的东西,参考了网上很多的教程,终于耗时三个多小时初步理解并编写了一个小demo,现在分享给大家,希望大家少走弯路.步骤如下: 1>像平常创建项目一样创建一个d ...

  2. MYSQL整理的语法

    MYSQL整理的语法 http://www.cnblogs.com/suoning/p/5744849.html

  3. jQuery triger与trigerHandler的区别

    trigger(event, [data]) 与 triggerHandler(event, [data]) 都是用于触发一个事件. 其两者的区别在于,如果触发的事件是有浏览器默认行为的,trigge ...

  4. 使用View为Data Source的Form开发要点

    (Data Source为View) 要点一:创建View的SQL语法 View的SQL里必须指定Form里唯一一个对其新增.修改.删除的基本表及其主键,其它表为辅助信息表,其字段仅用来在Form里显 ...

  5. logging日志模块

    为什么要做日志: 审计跟踪:但错误发生时,你需要清除知道该如何处理,通过对日志跟踪,你可以获取该错误发生的具体环境,你需要确切知道什么是什么引起该错误,什么对该错误不会造成影响. 跟踪应用的警告和错误 ...

  6. oracle 做算法 数据为空时,默认为0

    SELECT NVL('',0) FROM DUAL 获取当前日期: SELECT SYSDATE FROM DUAL 当前日期-某个日期差的天数 SELECT  TO_NUMBER(SYSDATE  ...

  7. 关于mysql 的一些零碎.

    /* 又在做自己以前做的事.总是拿以前的眼光来看现在,导致了其实自己已经很low,但是还觉得自己很xxx. 好吧,最近开始PHP审计.jishigou!!!!!! */ 查看日志情况. show va ...

  8. swift 代码添加lable

    let lable1 = UILabel(frame: CGRect(x: CGFloat(self.view.bounds.width/2-20), y: CGFloat(history.frame ...

  9. Spring +SpringMVC 实现文件上传功能。。。

    要实现Spring +SpringMVC  实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...

  10. LightOJ 1094 - Farthest Nodes in a Tree(树的直径)

    http://acm.hust.edu.cn/vjudge/contest/121398#problem/H 不是特别理解,今天第一次碰到这种问题.给个链接看大神的解释吧 http://www.cnb ...