kiran kumar kiran kumar - 4 months ago 133
iOS Question

How to Upload Multipart data of image in base64 Using afnetworking

I have used the following code but the response which i get is

java.lang.NullPointerException
&
INTERNAL_SERVER_ERROR
I tried many different methods but unable to fix it please help in fixing this.

Getting the Image from the Image picker

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
Profilebackground.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
NSURL *resourceURL;
UIImage *image =[[UIImage alloc] init];

image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];

NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

imageName = [imagePath lastPathComponent];

resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];



NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:@"."].location+1 ];

if ([extensionOFImage isEqualToString:@"JPG"])
{
imageData =UIImageJPEGRepresentation(image, 1.0);
base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
extension=@"image/jpeg";

}

else
{
imageData = UIImagePNGRepresentation(image);
base64 = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
extension=@"image/png";
}

int imageSize=imageData.length/1024;

NSLog(@"imageSize--->%d", imageSize);
if (imageName!=nil) {
NSLog(@"imageName--->%@",imageName);
}
else
{
NSLog(@"no image name found");
}


Send the Image to server

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"https://blahblahblah.com/uploadProfileImg?userId=1" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//NSData *pngData = [[NSData alloc] initWithBase64EncodedString:base64 options:1];
[formData appendPartWithFileData:imageData
name:@"key"
fileName:imageName mimeType:extension];

} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSLog(@"error: %@",error);
// NSHTTPURLResponse *response = (NSHTTPURLResponse *)operation.response;
NSLog(@"statusCode: %ld", (long)response.statusCode);
NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSLog(@"Error Response:%@",ErrorResponse);
}];

Answer

You can just use the appendPartWithFileData:name:fileName:mimeType: method of the AFMultipartFormData class.

For instance:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager POST:@"https://blahblahblah.com/imageupload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData
                                name:@"key name for the image"
                            fileName:photoName mimeType:@"image/jpeg"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
}];