AFNetworking 2.0 API用于多部分表单上传

时间:2013-10-18 06:44:46

标签: ios afnetworking afnetworking-2

用于上传多部分表单的AFNetworking 2.0方法是什么,即使用AFHTTPSessionManagerAFHTTPRequestOperationManager

以下是一些AFNetworking< 2.0代码:

[[ASAPIClient sharedClient] enqueueHTTPRequestOperation:[[ASAPIClient sharedClient] HTTPRequestOperationWithRequest:[[ASAPIClient sharedClient] multipartFormRequestWithMethod:@"POST" path:@"/some/url" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if(_selectedPhoto.image)
        {
            NSData *imageData = UIImageJPEGRepresentation([_selectedPhoto image], 0.5);

            [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
        }
    }] success:^(AFHTTPRequestOperation *operation, id responseObject) {

        if([[responseObject objectForKey:@"result"]isKindOfClass:[NSDictionary class]] )
        {

        }

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

    }]];

1 个答案:

答案 0 :(得分:3)

ASAPIManagerAFHTTPRequestOperationManager

的子类
[[ASAPIManager sharedManager] POST:@"/some/url" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if(_profileImageView.image){
            [formData appendPartWithFileData:UIImageJPEGRepresentation(_profileImageView.image, 0.5) name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

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

    }];