2013-10-03 26 views
1

在這裏,我需要通過使用post方法上傳mp3音頻,但是寫了用於上傳音頻的代碼,但是我不知道如何包含參數。 請檢查我的代碼並幫助我附加參數。ios - 在post方法中上傳音頻和附加參數

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 

我的參數是用戶名和session.Could任何機構幫我添加這些參數和值

+0

沒有ü在這樣做成功嗎?我想上傳2個音頻文件,1個圖像和3個字符串.. plz幫助在這... – Mak13

回答

1

您可以使用ASIHTTPRequest發送Audiofiles和其他參數。它易於使用。僅有2線code。它的幫助了我很多:-)

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

只是前行// final boundary加上這些行:

//Append userid 
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:[[NSString stringWithFormat:@"%@\r\n", myUserID] dataUsingEncoding:NSUTF8StringEncoding]]; 

在使用這些三線並結束追加更多的領域,例如會話字段。

+0

謝謝你的回答 – dineshprasanna