我使用的最新版本的AFNetworking 2.3.1 该版本已经没有AFHttpClient这个类了 于是使用wiki中的这种方式上传图片
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
UIImage *image = [UIImage imageNamed:@"xxxx"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(image)
name:@"avatar"
fileName:@"avatar.png"
mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
但是服务器一直接受不到图片数据。这里的name与filename参数有什么要求吗?
name:@"avatar"
This field must be consistent with the server
Replace fileName with filePath, for example fileName:filePath, provided that the data is read from a file
Is the problem solved? Please reply if you see it, I want to know too!
Please answer! Thank you
Server side:
file.getFile("avatar");