3

上传多部分表单的 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) {

    }]];
4

1 回答 1

3

ASAPIManager是一个子类AFHTTPRequestOperationManager

[[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) {

    }];
于 2013-10-18T06:51:38.143 回答