Code cracker
Code cracker

Reputation: 3166

How to send more then one image to server

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.

Upvotes: 0

Views: 942

Answers (2)

if-else-switch
if-else-switch

Reputation: 977

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.

Upvotes: 0

Harish Kanojiya
Harish Kanojiya

Reputation: 26

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];

}

Upvotes: 1

Related Questions