在一个请求中上传多个图像

我想在单个服务请求中上传多个图片。 我怎样才能做到这一点。 截至目前,我能够像上传单个图像

NSData *imageData = UIImageJPEGRepresentation(image, 90); NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; NSMutableData *postBody = [NSMutableData data]; // file [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\”%@\”\r\n”,@“imagename”]] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[NSData dataWithData:imageData]]; [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

现在我想上传两个图像到服务器,我怎么能做到这一点。

你的答案在这里。 它已经过testing,我正在使用下面的方法来上传多个图像以及其他参数。

 - (void)uploadMultipleImageInSingleRequest { NSString *returnString; NSDictionary *aParametersDic; // It's contains other parameters. NSDictionary *aImageDic; // It's contains multiple image data as value and a image name as key NSString *urlString; // an url where the request to be posted NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:url] ; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *postbody = [NSMutableData data]; NSString *postData = [self getHTTPBodyParamsFromDictionary:aParametersDic boundary:boundary]; [postbody appendData:[postData dataUsingEncoding:NSUTF8StringEncoding]]; [aImageDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { if(obj != nil) { [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"PostedImage\"; filetype=\"image/png\"; filename=\"%@\"\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [postbody appendData:[NSData dataWithData:obj]]; } }]; [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:postbody]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; } 

 -(NSString *) getHTTPBodyParamsFromDictionary: (NSDictionary *)params boundary:(NSString *)boundary { NSMutableString *tempVal = [[NSMutableString alloc] init]; for(NSString * key in params) { [tempVal appendFormat:@"\r\n--%@\r\n", boundary]; [tempVal appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@",key,[params objectForKey:key]]; } return [tempVal description]; } 

一个选项使用AFNetworking上传多个图像

 //create image data UIImage *image = ...... NSData *imageData = UIImageJPEGRepresentation(image, 0.5); UIImage *image2 = ...... NSData *imageData2 = UIImageJPEGRepresentation(image2, 0.5); //Now add to array and also create array of images data NSArray *arrImagesData = [NSArray arrayWithObjects:imageData,imageData2,nil]; //Create manager AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //parameters if any NSDictionary *parameters = ....... //Now post [manager POST:@"your url here" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //add img data one by one for(int i=0; i<[arrImagesData count];i++) { NSData *imageData = arrImagesData[i]; NSString *strName = [NSString stringWithFormat:@"name%d",i] [formData appendPartWithFormData:imageData name:strName]; } } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; 
 #define kStartTag @"--%@\r\n" #define kEndTag @"\r\n" #define kContent @"Content-Disposition: form-data; name=\"%@\"\r\n\r\n" #define kBoundary @"---------------------------14737809831466499882746641449" -(void)uploadImageonServers { NSMutableURLRequest *request = nil; NSLog(@"image upload"); NSMutableData *body = [NSMutableData data]; request = [[NSMutableURLRequest alloc] init]; NSString *requestURL = [NSString stringWithFormat:@"http://192.168.1.101/gmento/index.php/api/professional/addServices"]; [request setURL:[NSURL URLWithString:requestURL]]; [request setHTTPMethod:@"POST"]; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBoundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; // user ID parameter [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kContent, @"professional_id"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"1232" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kEndTag] dataUsingEncoding:NSUTF8StringEncoding]]; // Grop ID parameter [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kContent, @"title"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"test" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kEndTag] dataUsingEncoding:NSUTF8StringEncoding]]; // prepration_type_id parameter [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kContent, @"price"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"test" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kEndTag] dataUsingEncoding:NSUTF8StringEncoding]]; // description parameter [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kContent, @"description"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"test" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:kEndTag] dataUsingEncoding:NSUTF8StringEncoding]]; NSMutableArray *arr_images=[[NSMutableArray alloc] init]; [arr_images addObject:[UIImage imageNamed:@"1.jpg"]]; [arr_images addObject:[UIImage imageNamed:@"1.jpg"]]; for (int i=0; i<arr_images.count;i++ ) { NSData *image_videoData;//isImageSet?imgMyProfPict.image:@"" image_videoData = UIImageJPEGRepresentation([arr_images objectAtIndex:i], 1.0); if (image_videoData) { // image File [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=imageName.jpeg\r\n", @"services_image[]"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:image_videoData]; [body appendData:[[NSString stringWithFormat:kEndTag] dataUsingEncoding:NSUTF8StringEncoding]]; } else { [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; } } for (int i=0; i<arr_images.count;i++ ) { NSData *image_videoData;//isImageSet?imgMyProfPict.image:@"" image_videoData = UIImageJPEGRepresentation([arr_images objectAtIndex:i], 1.0); if (image_videoData) { // image File [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=imageName.jpeg\r\n", @"glimpses_image[]"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:image_videoData]; [body appendData:[[NSString stringWithFormat:kEndTag] dataUsingEncoding:NSUTF8StringEncoding]]; } else { [body appendData:[[NSString stringWithFormat:kStartTag, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; } } // close form [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", kBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:body]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSError *error; id receivedData = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:&error]; NSDictionary *dicResponse = (NSDictionary *)receivedData; if ([[dicResponse valueForKeyPath:@"status"] intValue] == 1) { } else { } } 
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setHTTPShouldHandleCookies:NO]; [request setTimeoutInterval:60]; [request setHTTPMethod:@"POST"]; NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; NSMutableData *body = [NSMutableData data]; NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; [parameters setValue:@"IOS Testing" forKey:@"subject"]; [parameters setValue:@"Message" forKey:@"message"]; for (NSString *param in parameters) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]]; } NSMutableArray *multipleArray=[[NSMutableArray alloc] init]; [multipleArray addObject:[UIImage imageNamed:@"images.jpeg"]]; [multipleArray addObject:[UIImage imageNamed:@"pic3.png"]]; for (int i=0; i<multipleArray.count;i++ ) { NSData *imageData; imageData = UIImageJPEGRepresentation([multipleArray objectAtIndex:i], 1.0); if (imageData) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"uploaded_file[]"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } else { [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } } [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; [request setURL:[NSURL URLWithString:BASEURL]]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; if ([httpResponse statusCode] == 200) { NSLog(@"success"); } }]; 

}