AFNetworking 2.0 API For Multi-part form upload

倾然丶 夕夏残阳落幕 提交于 2020-01-04 02:16:04

问题


What is the AFNetworking 2.0 method for uploading a multipart form i.e. With AFHTTPSessionManager or AFHTTPRequestOperationManager

Here is an example of some AFNetworking < 2.0 code:

[[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:


ASAPIManager is a subclass of 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) {

    }];


来源:https://stackoverflow.com/questions/19443241/afnetworking-2-0-api-for-multi-part-form-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!