0

I'm able to send single image to the server. Now I need to modify the code to send two images in two different urls . The code i've used to send singe image is

NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",requestString];
NSLog(@"url1%@",url);
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];

// Create 'POST' MutableRequest with Data and Other Image Attachment.

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",   boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

 NSData *data = UIImageJPEGRepresentation(chosenImage1, 0.2f);
 [request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
 NSMutableData *body = [NSMutableData data];
 [body appendData:[NSData dataWithData:data]];
 [request setHTTPBody:body];

Help me, Thanks in advance for everyone.

if-else-switch
  • 977
  • 1
  • 7
  • 24
Code cracker
  • 3,105
  • 6
  • 37
  • 67

2 Answers2

1

Try Following Code Make Sure You assign Images and Urls properly

UIImage * image1 ;
UIImage * image2;

NSString * imageUrl1;
NSString * imageUrl2;



NSMutableArray * arrImageData=[[NSMutableArray alloc]initWithObjects:image1,image2,nil];

NSMutableArray * arrImageUrls=[[NSMutableArray alloc]initWithObjects:imageUrl1,imageUrl2,nil];

for(int i=0; i < arrImageData.count ; i++){


    NSString *url=[NSString stringWithFormat:@"http://37.187.152.236/UserImage.svc/InsertObjectImage?%@",[arrImageUrls objectAtIndex:i]];
    NSLog(@"url1%@",url);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];

    // Create 'POST' MutableRequest with Data and Other Image Attachment.

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",   boundary];
    [request setValue:contentType forHTTPHeaderField:@"Content-Type"];

    UIImage * chosenImage1=[arrImageData objectAtIndex:i];

    NSData *data = UIImageJPEGRepresentation(chosenImage1, 0.2f);
    [request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
    NSMutableData *body = [NSMutableData data];
    [body appendData:[NSData dataWithData:data]];
    [request setHTTPBody:body];

}
  • i need to use two urls . for it u used mutable array, its okay but whats the need one more url inside. – Code cracker Jun 28 '14 at 05:49
  • your answer is very closer to the solution but not properly formatted. can u please improve this answer – Code cracker Jun 28 '14 at 06:14
  • what ever is stored in arrImageUrls its in form of NSString and "request setURL: " needs NSURL as parameter so just 1. We need to get single object from array 2. Then pass it to request by url one by one all objects(Image Urls) will be posted in for loop – Harish Kanojiya Jun 30 '14 at 05:48
0

For sending multiple images in the same url at a time to the server you have to use base64 conversion of images and then add all converted image's string into the JSON. After JSON formatting you can simply send those images to the server. Same in the server side you have to decode those base64 converted image's string to image data and save it to the directory. For base64 Encode and Decode please refer to this link

For sending two images to two different url's you can proceed with Harish Kanojiya answer.

Community
  • 1
  • 1
if-else-switch
  • 977
  • 1
  • 7
  • 24