gpt4 book ai didi

ios - 在 post 方法中上传音频和附加参数

转载 作者:行者123 更新时间:2023-11-29 10:54:10 25 4
gpt4 key购买 nike

这里我需要使用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

我的参数是userid和session。谁能帮我添加这些参数和值

最佳答案

您可以使用 ASIHTTPRequest用于发送音频文件和其他参数。它易于使用。只有 2 行 code .它帮了我很多:-)

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

关于ios - 在 post 方法中上传音频和附加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19156969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com