[英]How to send wav audio file using javascript and webapi c#
我需要将一个音频wav文件发送到webapi控制器,用于微软bing语音api调用。 我所做的是,
录制的音频在客户端使用javascript转换为base64数据
使用ajax调用调用webapi控制器并发送base64音频数据。
3.在webapi控制器中,将base64数据转换为字节并发送到restpi(microsoft)。
请帮助我如何正确地完成所有这些步骤
ajax电话,
$.ajax({
url: 'http://localhost:49818/api/voice',
type: 'POST',
data: base64Data,
dataType: 'json',
contentType: "application/json",
success: function (data) {
alert(data);
},
webapi控制器方法
string b64 = Request.Content.ReadAsStringAsync().Result;
//string text = System.IO.File.ReadAllText(@"D:\\base64.txt");
var client = new HttpClient();
byte[] toBytes1 = Encoding.ASCII.GetBytes(b64);
var uri = "https://westus.api.cognitive.microsoft.com/spid/v1.0/identificationProfiles/a1cb4a95-9e09-4f54-982b-09632aee7458/enroll?shortAudio=true";
HttpResponseMessage response;
byte[] toBytes = Encoding.ASCII.GetBytes(b64);
using (var content = new ByteArrayContent(toBytes))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
//content.Headers.ContentType = new MediaTypeHeaderValue("audio/wav");
response = await client.PostAsync(uri, content);
}
contentType
是您要发送的数据类型,因此application / json; 默认为application/x-www-form-urlencoded; charset=UTF-8
application/x-www-form-urlencoded; charset=UTF-8
。
如果使用application/json
,则必须使用JSON.stringify()
才能发送JSON对象。
JSON.stringify()
将javascript object
为json
文本并将其存储在字符串中。
data: JSON.stringify({"mydata":base64Data}),
在你的控制器中你必须传递一个名为myData
的参数。这样的东西:
C#
public ActionResult MyMethod(string mydata){
//code
}
UPDATE
$.ajax({
url: 'http://localhost:49818/api/voice',
type: 'POST',
data:{"mydata":base64Data},
dataType: 'json',
success: function (data) {
alert(data);
},
});
C#
public async void Post([FromBody] string mydata){
//code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.