我正在开发一个iOS应用程序。我必须以编程方式将音频文件发送到我们的服务器。我正在使用以下代码将示例wav音频文件发送到服务器。我们的服务器只接受符号字节数组格式的音频文件进行接收。iPhone:错误上传音频wav字节数据到服务器
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"Crash" ofType:@"wav"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSLog(@"wavbundlepath: %@",wavbundlepath);
NSData *bytes = [wavbundlepath dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"bytes: %@",bytes);
NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", @"http://www.mywebserver/api/UploadFile?Name="];
[urlstr appendString:@"crash"];
[urlstr appendFormat:@"&MemberID=%d", 0];
[urlstr appendFormat:@"&Type=%@",@"Recording"];
[urlstr appendFormat:@"&client=%@",@"ios"];
NSLog(@"urlstr.......%@",urlstr);
NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
[recordRequest setURL:[NSURL URLWithString:urlstr]];
[recordRequest setHTTPMethod:@"POST"];
[recordRequest setValue:recordPostLength forHTTPHeaderField:@"Content-Length"];
[recordRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[recordRequest setHTTPBody:bytes];
NSURLResponse *recordResponse;
NSError *recordError;
NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
NSLog(@"recordResp:%@", recordResp);
但问题是,总是接收“输入字符串不正确”。响应错误。
我不知道上传音频字节到服务器。有人可以检查此代码,并告诉我这是有效的代码或不上传一个WAV音频字节数组到服务器?
谢谢。
更新的代码
我想下面的代码作为我的服务器端的工程师没有说有任何其他的东西张贴的身体,并能正常工作并得到积极响应。但是,服务器无法使用我要发送的格式NSData字节(32位元素),因为服务器端的实现仅接收字节数组或字节数据格式。
NSURL *urlPath = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"Temp" ofType:@"wav"]];
NSString *wavbundlepath = [urlPath absoluteString];
NSLog(@"wavbundlepath: %@",wavbundlepath);
NSData *bytes=[NSData dataWithContentsOfURL:urlPath];
NSLog(@"bytes: %@",bytes);
NSString *recordPostLength = [NSString stringWithFormat:@"%d", [bytes length]];
NSMutableString *urlstr = [NSMutableString stringWithFormat:@"%@", @"http://www.myserver.com/api/UploadFile?Name="];
[urlstr appendString:@"Temp"];
[urlstr appendFormat:@"&MemberID=%d", 0];
[urlstr appendFormat:@"&Type=%@",@"Recording"];
[urlstr appendFormat:@"&client=%@",@"ios"];
NSLog(@"urlstr.......%@",urlstr);
NSMutableURLRequest *recordRequest = [[NSMutableURLRequest alloc] init] ;
[recordRequest setURL:[NSURL URLWithString:urlstr]];
NSInputStream *dataStream = [NSInputStream inputStreamWithData:bytes];
[recordRequest setHTTPBodyStream:dataStream];
[recordRequest setHTTPMethod:@"POST"];
NSURLResponse *recordResponse;
NSError *recordError;
NSData *recordResponseData = [NSURLConnection sendSynchronousRequest:recordRequest returningResponse:&recordResponse error:&recordError];
NSString *recordResp = [[NSString alloc]initWithData:recordResponseData encoding:NSUTF8StringEncoding];
NSLog(@"recordResp:%@", recordResp);
recordResponceJson = [recordResp JSONValue];
NSLog(@"recordResponceJson = %@",recordResponceJson);
recId = [recordResponceJson valueForKey:@"ID"];
NSLog(@"recId....%@", recId);
有人请指导我,我怎么可以在这个http文章中发送字节数组?
好的谢谢,还有一个问题,是NSData无符号字节数组? – Stella
是的,NSData是字节的集合。因此,当您将它们添加为POST主体时,它们将作为字节流添加到请求中。我强烈建议你使用setHTTPBodyStream:方法作为流发送主体,而不是将它们加载到内存中。流方法将自动填充HTTP请求的内容长度头,因此您不需要进行该计算。 –
嗨,我问我的服务器端工程师,他表示帖子主体应该只包含文件,没有其他的东西。所以,我删除了其他东西,并尝试使用setHttpBodyStream(),如你所建议的。请检查上述问题的更新代码。但是,它没有做任何事情。已收到空的答复。 – Stella