加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何通过http发送音频文件从ios发送到服务器?

发布时间:2020-12-15 01:49:42 所属栏目:百科 来源:网络整理
导读:我有两个功能记录,并将此录制的声音发布到服务器. 这是我用来发布到服务器的以下代码 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"recordedTmpFile" ofType:@"caf"]; NSURL *file =[[NSURL alloc] initFileURLWithPath:filePath]; NSStr
我有两个功能记录,并将此录制的声音发布到服务器.

这是我用来发布到服务器的以下代码

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到string

NSString* newStr = [NSString stringWithUTF8String:[postData  bytes]];

// http帖子

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:方法,允许您将文件发布到服务器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读