AFNetworking 3.0 多张图片上传接口

2017-04-21  本文已影响25人  行走的风车

自己项目里的调用实例,直接上代码了

-(void)httpphototest
{
    NSString *url = [NSString stringWithFormat:@"%@%@",NETADDRESS,@"AAA/BBB"];
    NSMutableDictionary* dic = [[NSMutableDictionary alloc]initWithCapacity:10];

    [dic setValue:@(2) forKey:@"flag"];
    [dic setValue:@(7) forKey:@"operId"];
    [dic setValue:@(1) forKey:@"afterFlag"];
    [dic setValue:@(3) forKey:@"updownName"];
    
    AFHTTPSessionManager* AFHttpManager = [[AFHTTPSessionManager alloc]init];
    AFHttpManager.requestSerializer = [AFHTTPRequestSerializer serializer];

    // 设置超时时间
    [AFHttpManager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    AFHttpManager.requestSerializer.timeoutInterval = 30.f;
    [AFHttpManager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    
    [AFHttpManager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
     {
         // 上传 多张图片
         UIImage *image1 = [UIImage imageNamed:@"IMG_1175.JPG"];
         UIImage *image2 = [UIImage imageNamed:@"IMG_1177.JPG"];
         
         NSArray *imageArray = [NSArray arrayWithObjects:image1,image2,nil];
         
         NSObject *firstObj = [imageArray objectAtIndex:0];
         if ([firstObj isKindOfClass:[UIImage class]]) {     // 图片
             for(NSInteger i=0; i<imageArray.count; i++) {
                 UIImage *eachImg = [imageArray objectAtIndex:i];
                 NSData *eachImgData = UIImageJPEGRepresentation(eachImg, 0.5);
                 /**参数
                  appendPartWithFileData:要上传的照片[二进制流]
                  name:服务器存你上传图片的文件夹名
                  fileName:文件名
                  mimeType:上传的文件的类型
                  */
                 [formData appendPartWithFileData:eachImgData name:@"myfiles" fileName:[NSString stringWithFormat:@"img%ld.jpg", i+1] mimeType:@"image/jpeg"];
             }
         }
         
     } progress:^(NSProgress * _Nonnull uploadProgress) {
         
         NSLog(@"---上传进度--- %@",uploadProgress);
         
     } success:^(NSURLSessionDataTask *task, id responseObject) {
         NSLog(@"success!!!!!!!\r\n");
     
     } failure:^(NSURLSessionDataTask *task, NSError *error) {
         NSLog(@"fail!!!!!!!!!\r\n");
     }];
}

上一篇下一篇

猜你喜欢

热点阅读