We are sending right post request to API by multi parting both the image data.So,there is an issue both image are not uploading, just a single image is uploading to server. I have checked the parameter name which are as same provided from API team, here is it:-
#define IMAGE_DP @"Content-Disposition: form-data; name=\"profileimage\"; filename=\"image.jpg\"
Content-Type: image/jpeg
"
#define IMAGE_COVER @"Content-Disposition: form-data; name=\"coverimage\"; filename=\"image.jpg\"
Content-Type: image/jpeg
"
Building request to upload two images in a single NSURLMutable request
if ([isprofilepicupdated isEqualToString:@"1"]) {
[postBody appendData:[IMAGE_DP dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:profileimage]];
[postBody appendData:[[NSString stringWithFormat:@"
--%@--
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([iscoverpicupdated isEqualToString:@"1"]) {
[postBody appendData:[IMAGE_COVER dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:coverimage]];
[postBody appendData:[[NSString stringWithFormat:@"
--%@--
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
So how can we append two images in single body of NSURLMutable request?
NSString *urlString = @"<PostImage-url>";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
for (int i=0;i<(YOUR _ARRAY).count;i++)
{
UIImage * myimg=[YOUR _ARRAY objectAtIndex:i];
NSData *ImageData = UIImagePNGRepresentation(myimg);
[body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"img1[]\"; filename=\".jpg\"
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:ImageData]];
[body appendData:[@"
" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"vin\"
"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//ASynchronous request
[[NSURLConnection alloc] initWithRequest:request delegate:self];