当前位置: 动力学知识库 > 问答 > 编程问答 >

IOS AFNetworking upload pictures to the server can not resolve base64

问题描述:

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

The same request using ASIHTTPReuqest framework upload base64 images on the server and parsing is successful. Can you give a suggestion or idea to fix the code with AFNetworking?

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: