1

这里我需要使用post方法上传mp3音频,但是写了上传音频的代码,但我不知道如何包含参数。请检查我的代码并帮助我附加参数。

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

我的参数是用户 ID 和会话。任何人都可以帮助我添加这些参数和值

4

2 回答 2

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]];

一遍又一遍地使用这 3 行来附加更多字段,例如会话字段。

于 2013-10-03T10:52:37.297 回答
1

您可以使用ASIHTTPRequest发送音频文件和其他参数。它易于使用。只需 2 行代码。它对我帮助很大 :-)

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setFile:@"YOUR FILE PATH WITH FILE EXTENSION" forKey:@"YOUR FILE KEY"];
于 2013-10-03T13:02:29.087 回答