Reputation: 2119
I want to upload two images to the server with following code can any one please help me, here is my code for post data
- (void)uploadPath:(NSString*)path withOptions:(NSDictionary*)options withImageData1:(NSData*)data1 ofImageName1:(NSString *)imageName1 andImageData2:(NSData*)data2 ofImageName2:(NSString *)imageName2 {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:baseURLString]];
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.0];
NSString *boundary = @"-------1234567";
[mutableRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
[mutableRequest setHTTPMethod:@"POST"];
NSMutableData *postbody = [NSMutableData data];
for(NSString *key in options)
{
NSString *value = [options objectForKey:key];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
if ([imageName1 isEqualToString:@"picture1.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture1\"; filename=\"%@\"\r\n",imageName1] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data1]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
//******************* Append 2nd Image ************************/
if ([imageName2 isEqualToString:@"picture2.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture2\"; filename=\"%@\"\r\n",imageName2] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data2]];
//******************* Append 2nd Image ************************/
[mutableRequest setHTTPBody:postbody];
urlConnection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self];
if (self.urlConnection) {
self.dataXml = [NSMutableData data];
}
self.urlConnection = nil;
[mutableRequest release];
[pool release];
}
Upvotes: 3
Views: 1895
Reputation: 243
- (NSString*) nameValString: (NSDictionary*) dict {//example CHLOVA_FORM_BOUNDARY is @"aaa"
NSArray* keys = [dict allKeys];
NSString* result = [NSString string];
int i;
for (i = 0; i < [keys count]; i++) {
result = [result stringByAppendingString:
[@"--" stringByAppendingString:
[CHLOVA_FORM_BOUNDARY stringByAppendingString:
[@"\r\nContent-Disposition: form-data; name=\"" stringByAppendingString:
[[keys objectAtIndex: i] stringByAppendingString:
[@"\"\r\n\r\n" stringByAppendingString:
[[dict valueForKey: [keys objectAtIndex: i]] stringByAppendingString: @"\r\n"]]]]]]];
}
return result;
}
- (void)sendPhoto{
NSString *param = [self nameValString:dic];//dic is post info like {lat=36;lng=120;}
NSString *footer = [NSString stringWithFormat:@"\r\n--%@--\r\n", CHLOVA_FORM_BOUNDARY];
param = [param stringByAppendingString:[NSString stringWithFormat:@"--%@\r\n", CHLOVA_FORM_BOUNDARY]];
param = [param stringByAppendingString:@"Content-Disposition: form-data; name=\"pic\";filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"];
NSData *jpeg = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:_path], 0.55);
//NSLog(@"jpeg size: %d", [jpeg length]);
NSMutableData *data = [NSMutableData data];
//img one
[data appendData:[param dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg];
[data appendData:[footer dataUsingEncoding:NSUTF8StringEncoding]];
//img two
[data appendData:[param2 dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg2];
[data appendData:[footer2 dataUsingEncoding:NSUTF8StringEncoding]];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", CHLOVA_FORM_BOUNDARY];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:@"POST"];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
//then do sending
}
Upvotes: 1
Reputation: 75058
One solution to see what is wrong:
ASIHttpRequest
You can also replace step (1) with a curl
command line that uploads multiple files to a server and record that traffic, but I'm not versed enough in curl
to say off the top of my head what that command would look like.
Upvotes: 3