简体   繁体   English

如何使用javascript和webapi c发送wav音频文件#

[英]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 , 我所做的是,

  1. Recorded audio converted to base64 data using javascript in the client side 录制的音频在客户端使用javascript转换为base64数据

  2. 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 objectjson文本并将其存储在字符串中。

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.

相关问题 Javascript将输入作为数组发送到C#WebAPI - Javascript send an input as array to a C# WebAPI 如何在C#中将字符串数据:audio / ogg; base64转换为文件wav - How to convert string data:audio/ogg;base64 to file wav in C# 如何使用 Javascript 将两个 audio.wav 文件合并/合并为一个文件 - How to combine/merge two audio .wav files into one file with Javascript 使用ffmpeg javascript将wav音频文件转换为pcm音频文件 - converting from wav audio file to pcm audio file using ffmpeg javascript 如何使用NodeJS将音频流另存为WAV文件 - How to save an audio stream as a wav file using NodeJS 使用 Angular/Javascript 在 Edge 中播放 WAV 音频文件 - Play WAV audio file in Edge with Angular/Javascript 如何使用 Reactjs 或 Javascript 转换 wav 文件中的音频块? - How to transform an audioblob in wav file using Reactjs or Javascript? JavaScript:如何将音频Blob分成1秒的块并使用recorder.js导出到WAV文件? - JavaScript : How to split an audio blob into 1 second chunks and export to wav files using recorder.js? 如何将.wav响应转换为Blob网址,以便我可以使用JavaScript在客户端播放音频 - How to convert .wav response in to blob url so that I can play as audio in client side using javascript 如何使用JavaScript将WAV Blob转换为其他音频类型? - How can I convert a wav blob to other audio types using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM