I want to know how to upload multiple images to a server using objective C. Here is the code snippet that I am trying to use.
-(void)uploadImage:(NSMutableArray *)image andMessageBO:(MessageBO *)message
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@PostImageServlet?",kPostImageUploadWebServiceURL]];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:url];
[postRequest setHTTPMethod:@"POST"];
[postRequest setTimeoutInterval:60.0];
NSString *stringBoundary = @"0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSData *imageData =UIImagePNGRepresentation(image);
NSMutableData *postBody = [[NSMutableData alloc] init];
[postBody appendData:[[NSString stringWithFormat:@"isFormField=true"]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"--%@
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@",@"Content-Disposition: form-data; name="userid"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%d",messageBO.messageId] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"
--%@
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="profilepic"; filename="%d_postImage.png"
",messageBO.messageId] dataUsingEncoding:NSUTF8StringEncoding]];
for (int i = 0; i < [image count]; i++)
{
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="profilepic"; filename="%d_postImage%d.png"
",messageBO.messageId,i] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[image objectAtIndex:i]];
}
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream
"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageData]];
[postBody appendData:[[NSString stringWithFormat:@"
--%@--
",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
DebugLog(@"IMge:%@",returnString);
// [self removeOfflineImage:expenseBO];
}
Thanks
See Question&Answers more detail:os