Multiple File uploading

Discussion in 'iPhone/iPad Programming' started by sonalisingh, Apr 26, 2009.

  1. macrumors newbie

    Joined:
    Apr 26, 2009
    #1
    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:mad:"POST"];
    [urlRequest setValue:[NSString stringWithFormat:mad:"multipart/mixed; boundary=%@", boundry] forHTTPHeaderField:mad:"Content-Type"];
    NSMutableData *postData = [NSMutableData data];


    for(int i=1;i<3;i++){
    NSMutableString *filename = [[NSMutableString alloc]init];
    [filename appendString:mad:"Recording"];
    [filename appendString:[NSString stringWithFormat:mad:"%d",i]];
    [filename appendString:mad:".txt"];
    NSMutableString *audioLoc = [[NSMutableString alloc] init];
    [audioLoc appendString:path];
    [audioLoc appendString:mad:"/"];
    [audioLoc appendFormat:mad:"%d",534];
    [audioLoc appendString:mad:"/Recording"];
    [audioLoc appendString:[NSString stringWithFormat:mad:"%d",i]];
    [audioLoc appendString:mad:".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:mad:"--%@\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:mad:"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", @"file",filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithString:mad:"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[NSData dataWithContentsOfFile:audioLoc]];




    [audioLoc release];
    }

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



    /*//To send a single file
    [postData appendData:[[NSString stringWithFormat:mad:"--%@\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:[[NSString stringWithFormat:mad:"Content-Disposition: form-data; name=\"%@\"; filename=\"file.bin\"\r\n\r\n", FORM_FLE_INPUT] dataUsingEncoding:NSUTF8StringEncoding]];
    [postData appendData:data];
    [postData appendData:[[NSString stringWithFormat:mad:"\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;

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

Share This Page