search

Home  >  Q&A  >  body text

ios - AFNetworking 2.3.1 上传图片 服务器无法接收到图片?

我使用的最新版本的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参数有什么要求吗?

阿神阿神2824 days ago837

reply all(5)I'll reply

  • 高洛峰

    高洛峰2017-04-17 13:08:20

    name:@"avatar"
    This field must be consistent with the server

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:08:20

    Replace fileName with filePath, for example fileName:filePath, provided that the data is read from a file

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:08:20

    Is the problem solved? Please reply if you see it, I want to know too!

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:08:20

    Please answer! Thank you

    reply
    0
  • 阿神

    阿神2017-04-17 13:08:20

    Server side: file.getFile("avatar");

    reply
    0
  • Cancelreply