Search code examples
iosobjective-cpostnsurlrequest

ios - upload audio and additional parameters in post method


Here I need to upload the mp3 audio by using the post method, but wrote the code for uploading the audio but i dont know how include the parameters. kindly check my code and help me to attach parameters.

NSString *baseurl = UploadAudio;
//    baseurl = [NSString stringWithFormat:@"%@user_id=%@&comments=%@&login_key=%@",baseurl,[[sharedvar userdict] objectForKey:@"user_id"],[self.audiodict objectForKey:@"comments"],[[sharedvar userdict] objectForKey:@"login_key"]];
    NSLog(@"baseurl :%@",baseurl);
    NSURL *dataURL = [NSURL URLWithString:baseurl];

    NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

    [dataRqst setHTTPMethod:@"POST"];

    NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
    [dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];

    NSMutableData *postBody = [NSMutableData data];

    // -------------------- ---- Audio Upload Status ---------------------------\\
    //pass MediaType file

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

    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; userfile=\"file upload\"; filename=\"uploadaudio.mp3\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: audio/mp3\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //*******************load locally store audio file********************//
    NSString *audioUrl = [[self audiodict] objectForKey:@"audiopath"];
    NSLog(@"audioUrl :%@",audioUrl);

    // get the audio data from main bundle directly into NSData object
    NSData *audioData;
    audioData = [[NSData alloc] initWithContentsOfFile:audioUrl];

    // add it to body
    [postBody appendData:audioData];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // final boundary
    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
//    [postBody appendData:postData];
    // add body to post

    [dataRqst setHTTPBody:postBody];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:dataRqst delegate:self];
    [connection start];  // connection

My parameters are userid and session.Could any body help me for adding these parameters and values


Solution

  • You can use ASIHTTPRequest for sending Audiofiles and additional parameters. It is vary easy to use. Just 2 line of code .It helped me a lot :-)

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setFile:@"YOUR FILE PATH WITH FILE EXTENSION" forKey:@"YOUR FILE KEY"];