如何通过http帖子将音频文件从ios发送到服务器?
问题描述:
我有两个功能记录并将此录制的声音发布到服务器。
i have two function recordsound and post this recorded sound to the server.
这是我用来发布到服务器的以下代码
this is the following code i used to post to the server
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 to string
//nsdata to string
NSString* newStr = [NSString stringWithUTF8String:[postData bytes]];
// http post
//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];
答
我正在使用 ASIHTTPRequest 的库。它有一个setFile:方法,允许您将文件发布到服务器。
I am using ASIHTTPRequest's library. It has a setFile: method to allow you posting a file to the server.