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];
}];
Asked
Active
Viewed 57 times
0

Rajat
- 10,977
- 3
- 38
- 55

Sachin Kumar
- 1
- 3
-
What are you asking? – Wai Ha Lee Dec 16 '15 at 05:03
-
What are you asking ?and please describe your issue... – Hardip Kalola Dec 16 '15 at 05:15
-
Sir Actually I am Asking This is My Code And Not Properly Add Image With Image Path Code Is Crash And Parameter Goes To Null (From LoginId) Resopnse Please Give Correct suggestion And I am Using Afnetworking I am Not getting Plaese Shere Me And help me – Sachin Kumar Dec 16 '15 at 05:19
-
possible duplicate [post data to PHP script from iOS](http://stackoverflow.com/questions/15589255/post-data-to-php-script-from-ios) – Dipen Panchasara Dec 16 '15 at 06:06
-
Acttually i have already used in my app afnetworking....I am new Please Can U send me the demo. – Sachin Kumar Dec 16 '15 at 06:18
1 Answers
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
-
-
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