I am trying to post two images to an url using multipart post request in Objective C , From my code I can able to send only one image, How to send two images to the url, using multipart / form data I have tried so many example for this bur I did not get the result I want
-(void)processPostMultipartRequestNew{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *urlStr = [self.dataModel.apiUrl
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
urlStr = [urlStr
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
urlStr= [urlStr stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
NSURL *reqUrl = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:reqUrl];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSDictionary *requestBody = self.dataModel.requestParams2;
UIImage *imageToUpload = [requestBody objectForKey:keyUploadImage];
NSDictionary *requestBody2 = self.dataModel.requestParams;
UIImage *imageToUpload2 = [requestBody2 objectForKey:keyUploadImage];
if(requestBody){
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 0);//no compression by default
NSData *imageData2 = UIImageJPEGRepresentation(imageToUpload2, 0);
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"uploadedfile1\"; filename=\".jpeg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"uploadedfile2\"; filename=\".jpeg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[NSData dataWithData:imageData2]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Text parameter1
NSString *param1 = @"111";
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"SyncId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:param1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
}
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:REQUEST_TYPE_POST];
[request setTimeoutInterval:60*4];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"completionHandler with response:%@",[NSHTTPURLResponse localizedStringForStatusCode:[(NSHTTPURLResponse*)response statusCode]]);
NSLog(@"reponse: %ld",(long)[(NSHTTPURLResponse*)response statusCode]);
NSInteger status = [(NSHTTPURLResponse*)response statusCode];
if(error){
NSLog(@"http request error: %@", error.localizedDescription);
// handle the error
}
else{
if (status == 201){
// handle the success
}
else{
NSLog(@"error");
} // handle the error
}
if(!data || data == nil)
return ;
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
NSDictionary *resultDict =(NSDictionary *) [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingAllowFragments
error:&error];
//NSDictionary *resultDict =(NSDictionary *) [NSJSONSerialization JSONObjectWithStream:data options:kNilOptions error:&error];
self.dataModel.responseData =resultDict;
NSLog(@"error is %@",error);
NSLog(@"data is %@",data);
{
if(!data || data == nil)
return ;
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
//for failed case
if(error || error != nil){
self.dataModel.responseFailed = YES;
self.dataModel.error = error;
NSLog(@"Result in error case %@",[[error userInfo] objectForKey:@"NSDebugDescription"]);
[self.reciever performSelectorOnMainThread:self.callBack withObject:self.dataModel waitUntilDone:YES];
}
else{
self.dataModel.responseFailed = NO;
self.dataModel.error = nil;
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(!([response rangeOfString:@".jpg"].location==NSNotFound))
{
NSString *res=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
self.dataModel.responseData =res;
[self.reciever performSelectorOnMainThread:self.callBack withObject:self.dataModel waitUntilDone:YES];
}
}
}
self.executing = NO;
[self setFinishedState:YES];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self.reciever performSelectorOnMainThread:self.callBack withObject:self.dataModel waitUntilDone:YES];
}];
}
Is this a right way to do? if not how can I get success in this. Thanks in advance.