[英]How to send wav audio file using javascript and webapi c#
i need to send an audio wav file to the webapi controller for the microsoft bing speech api calls . 我需要将一个音频wav文件发送到webapi控制器,用于微软bing语音api调用。 what i have done is ,
我所做的是,
Recorded audio converted to base64 data using javascript in the client side 录制的音频在客户端使用javascript转换为base64数据
invoked webapi controller using ajax call and sends the base64 audio data as well. 使用ajax调用调用webapi控制器并发送base64音频数据。
3.in webapi controller , converted the base64 data to bytes and sends to the restpi (microsoft). 3.在webapi控制器中,将base64数据转换为字节并发送到restpi(microsoft)。
please help me how i can do all these steps correctly 请帮助我如何正确地完成所有这些步骤
ajax call , ajax电话,
$.ajax({
url: 'http://localhost:49818/api/voice',
type: 'POST',
data: base64Data,
dataType: 'json',
contentType: "application/json",
success: function (data) {
alert(data);
},
webapi controller method 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
is the type of data you're sending, so application/json; contentType
是您要发送的数据类型,因此application / json; The default is application/x-www-form-urlencoded; charset=UTF-8
默认为
application/x-www-form-urlencoded; charset=UTF-8
application/x-www-form-urlencoded; charset=UTF-8
. application/x-www-form-urlencoded; charset=UTF-8
。
If you use application/json
, you have to use JSON.stringify()
in order to send JSON object. 如果使用
application/json
,则必须使用JSON.stringify()
才能发送JSON对象。
JSON.stringify()
turns a javascript object
to json
text and stores it in a string. JSON.stringify()
将javascript object
为json
文本并将其存储在字符串中。
data: JSON.stringify({"mydata":base64Data}),
In your controller you have to pass a parameter called myData
.Something like this: 在你的控制器中你必须传递一个名为
myData
的参数。这样的东西:
C# C#
public ActionResult MyMethod(string mydata){
//code
}
UPDATE UPDATE
$.ajax({
url: 'http://localhost:49818/api/voice',
type: 'POST',
data:{"mydata":base64Data},
dataType: 'json',
success: function (data) {
alert(data);
},
});
C# C#
public async void Post([FromBody] string mydata){
//code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.