Afnetworking: IOS AFNetworking upload pictures to the server.The request is successful,But it can not parse base64 image

Created on 18 Mar 2015  Â·  1Comment  Â·  Source: AFNetworking/AFNetworking

IOS AFNetworking upload pictures to the server.The request is successful,But it can not parse base64 image.Then same request using ASIHTTPReuqest framework upload base64 images on the server side can parse is successful.Can you give a suggestion or idea?

AFNworking request Code:

UIImage *img = [avatar imageWithImageSimple:avatar scaledToSize:EZSIZE(320, 320)];

NSData* imageData = UIImageJPEGRepresentation(img, 0.5);

NSString *strBase64 = [imageData base64String];

NSDictionary * object = @{
           @"firstname"  :@"nameTest",
           @"sex"        :@"male",
           @"user_id"    :@"1234556",
           @"avatar"     :strBase64,
           @"phone"      :@"15899402939"
           };

NSMutableDictionary *parameter = [[NSMutableDictionary alloc] init];

[parameter setObject:@1 forKey:@"app"];

[parameter setObject:@"save_settings" forKey:@"m_action"];

[parameter setObject:[[object JSONRepresentation] forKey:@"m_param"]];

NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"
                                                                                   URLString:@"http://test..."
                                                                                  parameters:parameter
                                                                   constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                                   } error:nil];



AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

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

}];

ASIHttpRequest request code:

UIImage *img = [avatar imageWithImageSimple:avatar scaledToSize:EZSIZE(320, 320)];
NSData* imageData = UIImageJPEGRepresentation(img, 0.5);
NSString *strBase64 = [imageData base64String];

NSDictionary * object = @{
                          @"firstname"  :@"nameTest",
                          @"sex"        :@"male",
                          @"user_id"    :@"1234556",
                          @"avatar"     :strBase64,
                          @"phone"      :@"15899402939"
                          };

NSString *parameter = @"app=1";

parameter = [[parameter stringByAppendingString:@"&m_action="] stringByAppendingString:@"save_settings"];

parameter = [[parameter stringByAppendingString:@"&m_param="] stringByAppendingString:[object JSONRepresentation]];

NSData *data = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

[request setDidFailSelector:@selector(handleRequestEventError:)];
[request setDidFinishSelector:@selector(parseAndSave:)];

[request addRequestHeader:@"Content-Length" value:[@([data length]) stringValue]];
[request setRequestMethod:@"POST"];
[request appendPostData:data];
[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];

[request setDelegate:self];


[_networkQueue addOperation:request];

Most helpful comment

This is not a support forum. For usage questions, please try Stack Overflow.

>All comments

This is not a support forum. For usage questions, please try Stack Overflow.

Was this page helpful?
0 / 5 - 0 ratings