如何通过http发送音频文件发布到ios服务器?
问题描述:
我有两个功能记录,并将此录制的声音发送到服务器。如何通过http发送音频文件发布到ios服务器?
这是下面的代码我用来张贴到服务器
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"recordedTmpFile" ofType:@"caf"];
NSURL *file =[[NSURL alloc] initFileURLWithPath:filePath];
NSString *filepath = [[NSBundle mainBundle] initWithContentsOfURL:recordedTmpFile];
NSData *postData = [NSData dataWithContentsOfFile:filePath];
// NSData的字符串
NSString* newStr = [NSString stringWithUTF8String:[postData bytes]];
// HTTP POST
NSMutableString *jsonRequest = [[NSMutableString alloc]init];
[jsonRequest appendString:newStr];
NSURL *url = [NSURL URLWithString:@"http address"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
答
首先你必须以二进制格式更改音频文件数据,然后通过http协议在网络中发送它。
然后在服务器端,您必须编写代码来接受该二进制文件,然后将其转换为适当的音频文件并将其存储在服务器上。
这是你如何能做到你的任务......