AFNetworking 2.0+ 上传单张/多张图片

1.单张图片上传


name: 处为url提供的参数名


NSDictionary *dic =@{参数};

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManageralloc] init];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

manager.responseSerializer.acceptableContentTypes =[NSSetsetWithObject:@"text/html"];

[managerPOST:url parameters:dicconstructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [formData appendPartWithFileData:UIImagePNGRepresentation(image)name:@"Filedata"fileName:@"test.jpg"mimeType:@"image/jpg"];

    }success:^(AFHTTPRequestOperation *operation,id responseObject) {

      

    }failure:^(AFHTTPRequestOperation *operation,NSError *error) { 

 

    }];




2.多张图片上传

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:url参数 constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        for (int i = 0; icount; i++) {

            UIImage *uploadImage = arrayImage[i];

            [formData appendPartWithFileData:UIImagePNGRepresentation(uploadImage) name:[NSString stringWithFormat:@"参数%d",i+1] fileName:@"test.jpg" mimeType:@"image/jpg"];

        }

    } error:nil];


AFHTTPRequestOperation *opration = [[AFHTTPRequestOperation alloc]initWithRequest:request];   

opration.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[opration setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {


}


你可能感兴趣的:(iOS开发)