[iOS]服务器使用php时多图片上传AFNetworking的设置

与java服务器不同 服务器使用php时

[formData appendPartWithFileData:UIImageJPEGRepresentation(_PhArray[i], 0.1f) name:@"file[]" fileName:[NSString stringWithFormat:@"%@.png", fileName] mimeType:@"Multipart/form-data"];

name参数不能设置成@"file" 需要设置为"file[]" 否则会使服务器解析不到文件数组 只能解析最后一张图片

具体代码如下:

#pragma mark - 发送数据
- (void)postData {
    NSMutableDictionary *infoDic = [NSMutableDictionary dictionary];
    
    NSDictionary* para = [NSDictionary dictionary];
    para = @{@"info" : infoDic.JSONString};
    
    NSString* URL = @"filesupload.php";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer new];
    
    [manager POST:[NSString stringWithFormat:@"%@%@",EXILE_BASE_API,URL] parameters:para constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (_PhArray.count) {
            for (int i = 0; i<_PhArray.count; i++) {
                NSString *fileName = [NSString stringWithFormat:@"%.f_%d",[[NSDate date]timeIntervalSince1970],i];
                [formData appendPartWithFileData:UIImageJPEGRepresentation(_PhArray[i], 0.1f) name:@"file[]" fileName:[NSString stringWithFormat:@"%@.png", fileName] mimeType:@"Multipart/form-data"];
            }
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"response ==> %@", dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        Exile_ALERT(nil, error.localizedDescription)
    }];
}

 

posted @ 2017-06-14 11:39  death3721  阅读(563)  评论(0编辑  收藏  举报