0
NSString *strurl = [NSString stringWithFormat:@"http://inveera.biz/lowkall_api/index.php/img"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=----------V2ymHFg03ehbqgZCaKO6jy"];
//[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:[NSNumber numberWithInteger:[self.loginId integerValue]] forKey:@"id"];
[parameters setValue:[NSNumber numberWithInteger:[[[NSUserDefaults standardUserDefaults] objectForKey:@"loginId"] integerValue]] forKey:@"id"];
[parameters setValue:selling.text forKey:@"name"];
[parameters setValue:@"image.jpg" forKey:@"file"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:strurl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSDictionary*    data1 = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
     NSLog(@"JSON: %@", data1);
        //[Utils stopActivityIndicatorInView:self.view];
     UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Congratulation" message:@"User Registered Successfully" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alrt show];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", [error localizedDescription]);
     //[Utils stopActivityIndicatorInView:self.view];
     UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
     [alrt show];

}];
Rajat
  • 10,977
  • 3
  • 38
  • 55

1 Answers1

0
NSString *strurl = [NSString stringWithFormat:@"http://inveera.biz/lowkall_api/index.php/img"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:[NSNumber numberWithInteger:[self.loginId integerValue]] forKey:@"id"];
[parameters setValue:[NSNumber numberWithInteger:[[[NSUserDefaults standardUserDefaults] objectForKey:@"loginId"] integerValue]] forKey:@"id"];
[parameters setValue:selling.text forKey:@"name"];

manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:strurl parameters:parameters  constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
 {
     NSData *imageData = UIImageJPEGRepresentation(YOUR_IMAGE_HERE, 0.5); // put your UIImage

     [formData appendPartWithFileData:imageData name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
 }
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
    NSDictionary*    data1 = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"JSON: %@", data1);
    //[Utils stopActivityIndicatorInView:self.view];
    UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Congratulation" message:@"User Registered Successfully" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alrt show];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
    NSLog(@"Error: %@", [error localizedDescription]);
    //[Utils stopActivityIndicatorInView:self.view];
    UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alrt show];

}];

I have added block for image uploading, you can not directly upload the image via dictionary.

Please try this. hope this helps

I have got this response when run the code

{
    "p_id" = 69;
    status = true;
}
Ashish Ramani
  • 1,214
  • 1
  • 17
  • 30
  • *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: body' Show this I am Not Getting – Sachin Kumar Dec 16 '15 at 06:12
  • please see edited answer... i have put the response that i got from server while run the above code successfully. – Ashish Ramani Dec 16 '15 at 06:24
  • NSData *imageData = UIImageJPEGRepresentation(YOUR_IMAGE_HERE, 0.5); is your image is nil here....?. when this image is nil then the error occurs. – Ashish Ramani Dec 16 '15 at 06:33
  • So what Will Be The solution – Sachin Kumar Dec 16 '15 at 07:08
  • I think i have to set image path or image UIImage *image = [[UIImage alloc] init]; //your image here NSData *imageData = UIImageJPEGRepresentation(image, 0.5); – Sachin Kumar Dec 16 '15 at 08:56
  • From Method of - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info crash my course – Sachin Kumar Dec 16 '15 at 08:57
  • will u please explain.... i am not getting UIImage *image = [[UIImage alloc] init]; //your image here NSData *imageData = UIImageJPEGRepresentation(image, 0.5) – Sachin Kumar Dec 16 '15 at 09:24
  • - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info this method – Sachin Kumar Dec 16 '15 at 09:52
  • did you got the image from above method? then pass this image to "NSData *imageData = UIImageJPEGRepresentation(image, 0.5)" line. create UIImage variable at top and use it in this view controller. nad remove thiss line "UIImage *image = [[UIImage alloc] init];" – Ashish Ramani Dec 16 '15 at 10:07