0

Here is what I have tried so far:

-(void)postMethod_Param 
{    
     NSString *urlString=@"http://192.168.1.139:49/api//Grievance/PostCreateRequest";
     NSString *bodydata =[NSString stringWithFormat:@"&user_fb_id=%@&status=%d",fbUserId,status];

}
jsondwyer
  • 437
  • 8
  • 18

2 Answers2

0

If you want to post data to server

Pick Image from Gallery and save it to array

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
  imageView.image=image;
  [arrayImage addObject:image];
  picker.delegate =self;
  [picker dismissViewControllerAnimated:YES completion:nil];
}

For saving image as path and also convert into path as string

for (UIImage *img in array_Image)
{
  int i=0;
  NSString *pathName =nil;
  NSString *file_name = [[self getCurrentDate]stringByAppendingString:[self getCurrentTime]];
  file_name =[file_name stringByAppendingPathExtension:@"jpeg"];
  NSArray *paths1 =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *basePath =([paths1 count] >i) ? [paths1 objectAtIndex:i] : nil;
  NSString *path = [basePath stringByAppendingPathComponent:@"Photo"];
  //Get Directory in FileManager
  NSFileManager *fileManager =[NSFileManager defaultManager];
  if ([fileManager fileExistsAtPath:path])
    return;
  [fileManager createDirectoryAtPath:path  withIntermediateDirectories:NO attributes:nil error:nil];
  pathName =[path stringByAppendingPathComponent:file_name];
  NSData *imgData =UIImageJPEGRepresentation(image, 0.4);
  [imgData writeToFile:pathName atomically:YES];
  [arryImagePath addObject:pathName];
}

I Call the below methods in file_name

+(NSString*)getCurrentTime
{
  //Get Current Time for saving Images
  NSString *path =nil;
  NSDateFormatter *timeFormatter =[[NSDateFormatter alloc]init];
  [timeFormatter setDateFormat:@"HH:mm:ss.SSS"];
  NSDate *now = [[NSDate alloc]init];

  NSString *str_time = [timeFormatter stringFromDate:now];
  NSString *curr_time;
  curr_time =[str_time stringByReplacingOccurrencesOfString:@"." withString:@""];

  path = [NSString stringWithFormat:@"%@",curr_time];

  return  path;
}
+(NSString*)getCurrentDate
{
  NSString *today =nil;

  NSDateFormatter *dateFormatter1;
  dateFormatter1 =[[NSDateFormatter alloc]init];
  [dateFormatter1 setDateFormat:@"d MMM yyyy"];
  NSDate *now =[[NSDate alloc]init];
  NSLocale *usLocale =[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
  [dateFormatter1 setLocale:usLocale];
  NSString *str_date =[dateFormatter1 stringFromDate:now];
  today=[NSString stringWithFormat:@"%@",str_date];
  return today;
}

In above code If you print the arryImagePath you can see the image path.Before that you need to allocate and initialize the arryImagePath and array_image.

For Posting data into Server

NSURL *url = [NSURL URLWithString:urlString];
NSString *output = [NSString stringWithContentsOfURL:url encoding:0 error:&error];
NSLog(@"%@",output);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

NSData *strJsondata=[NSData dataWithBytes:[output UTF8String] length:[output length]];
//If the response is dictionary......
NSDictionary *responseDict = strJsondata ? [NSJSONSerialization JSONObjectWithData:strJsondata options:0 error:&error] : nil;
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • how to convert image and also voice clip in to file formate and upload to server i was success sending the text from text field,but i am failing to submit images and voice clips to server – Sitaram Naidu Jul 21 '16 at 12:40
  • From gallery i need to submit the image to server in file format – Sitaram Naidu Jul 21 '16 at 12:56
0

To upload a file on server you need to make a multipart request which I suggest you to use AFNetworking to do that ! In case you need some multipart example using AFNetworking you can see this or (Upload an image with AFNetworking 2.0) link. Hope this links could help you.

Update : To upload Image and Voice on server you need to convert them into data file, his is how you can convert image to data and upload it on server :

 NSData *imageData = UIImageJPEGRepresentation(selectedImage, 1.0f);

Then you need to make a request :

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:stringURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    // If you have image you need to do this :
    NSString *fileName = @"Some name";
    if (imageData) [formData appendPartWithFileData:imageData name:@"photo" fileName:fileName mimeType:@"image/jpeg"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Everything is ok
    NSLog(@"Success (%d): %@\n\n", (int) operation.response.statusCode, responseObject);
    if (success) success(responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    // Ops ! we have error
    NSLog(@"Failure (%d): %@\n\n", (int) operation.response.statusCode, [error localizedDescription]);
    if (failure) failure(nil);
}];

Important : pay attention on mime Type and name , they must be the same name and type requested by server.

Community
  • 1
  • 1
Sattar
  • 393
  • 5
  • 18