IOS AFNetworking上传图片到服务器无法解析base64

时间:2015-03-18 02:18:54

标签: ios base64 afnetworking

IOS AFNetworking将图片上传到服务器。请求成功,但服务器无法解析base64映像。

使用ASIHTTPReuqest框架上的相同请求在服务器上上传base64图像并且解析成功。你能给出一个建议或想法来修复AFNetworking的代码吗?

AFNworking请求代码:

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请求代码:

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];

0 个答案:

没有答案