PDA

View Full Version : Multiple File uploading




sonalisingh
Apr 27, 2009, 12:03 AM
Problem uploading multiple files on server. I am trying to upload multiple files on the server ,when i try this single file gets uploaded but multiple files do not.

I am using the following code but only single file gets uploaded .Please help me out.

********************************************************
NSArray *filePaths = NSSearchPathForDirectoriesInDomains (
NSDocumentDirectory,
NSUserDomainMask,
YES
);
NSString *path = [filePaths objectAtIndex: 0];


NSMutableURLRequest *urlRequest =[NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:[NSString stringWithFormat:@"multipart/mixed; boundary=%@", boundry] forHTTPHeaderField:@"Content-Type"];
NSMutableData *postData = [NSMutableData data];


for(int i=1;i<3;i++){
NSMutableString *filename = [[NSMutableString alloc]init];
[filename appendString:@"Recording"];
[filename appendString:[NSString stringWithFormat:@"%d",i]];
[filename appendString:@".txt"];
NSMutableString *audioLoc = [[NSMutableString alloc] init];
[audioLoc appendString:path];
[audioLoc appendString:@"/"];
[audioLoc appendFormat:@"%d",534];
[audioLoc appendString:@"/Recording"];
[audioLoc appendString:[NSString stringWithFormat:@"%d",i]];
[audioLoc appendString:@".txt"];
NSData *file = [NSData dataWithContentsOfFile:audioLoc];
ASSERT(file);
if (!file) {
NSLog(@"file not found");
}
if ([file length] == 0) {
NSLog(@"file lenghth is zero");
}

[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", @"file",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithContentsOfFile:audioLoc]];




[audioLoc release];
}

[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];



/*//To send a single file
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"file.bin\"\r\n\r\n", FORM_FLE_INPUT] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:data];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
//To send a single file*/
NSString *s = [[NSString alloc] initWithData:postData encoding:NSASCIIStringEncoding];
NSLog(@"The request body \n%@", s);

[urlRequest setHTTPBody:postData];
return urlRequest;

***************************************************