如何通过http帖子将音频文件从ios发送到服务器? [英] how to send audio file through http post to a server from ios?

查看:114
本文介绍了如何通过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.

这篇关于如何通过http帖子将音频文件从ios发送到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆