4

i need to send an audio wav file to the webapi controller for the microsoft bing speech api calls . what i have done is ,

  1. Recorded audio converted to base64 data using javascript in the client side

  2. invoked webapi controller using ajax call and sends the base64 audio data as well.

    3.in webapi controller , converted the base64 data to bytes and sends to the restpi (microsoft).

please help me how i can do all these steps correctly

ajax call ,

$.ajax({
            url: 'http://localhost:49818/api/voice',
            type: 'POST',
            data: base64Data,
            dataType: 'json',
            contentType: "application/json",
            success: function (data) {

                alert(data);
            },

webapi controller method

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);


            }
Ramseen Ramsi
  • 125
  • 1
  • 11

1 Answers1

0

contentType is the type of data you're sending, so application/json; The default is 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.

JSON.stringify() turns a javascript object to json text and stores it in a string.

data: JSON.stringify({"mydata":base64Data}),

In your controller you have to pass a parameter called myData.Something like this:

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
}
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • ok its done , then how can i convert the base64 data to bytes , byte[] toBytes1 = Encoding.ASCII.GetBytes(b64); .. this line getting wrong bytes – Ramseen Ramsi May 10 '17 at 10:28
  • Have a look here: http://stackoverflow.com/questions/6733845/c-sharp-convert-a-base64-byte – Mihai Alexandru-Ionut May 10 '17 at 10:39
  • getting null value in web api controller , here mydata is null, how can i send my data from ajaxcall , i used data: JSON.stringify({"mydata":base64Data}) .. `//POST: api/voice public async void Post([FromBody] string mydata) { // code }` – Ramseen Ramsi May 11 '17 at 05:49
  • @RamseenRamsi, do you add `[HttpPost]` verb for your method ? – Mihai Alexandru-Ionut May 11 '17 at 06:06
  • yes i added now but still its returns null value , but when i add like this my method `string b64 = Request.Content.ReadAsStringAsync().Result;` i get data which i send from ajax but not much as accurate , it seems to be corrupted. – Ramseen Ramsi May 11 '17 at 06:22
  • your using mvc controller action method but mine is webapi controller `//POST: api/voice [HttpPost] public async void Post([FromBody] string mydata) { try { string b64 = Request.Content.ReadAsStringAsync().Result; var client = new HttpClient();` may be thats why ? its not able to get the data in paramaeter (string mydata) ?? – Ramseen Ramsi May 11 '17 at 07:09
  • so it wont work with webapi right ? is there any other way of code method where i can execute my scenario ? – Ramseen Ramsi May 11 '17 at 08:38
  • Now ajax call must send the correct value to web api controller. – Mihai Alexandru-Ionut May 11 '17 at 08:49
  • that part is done , ajax send base64 data correctly , but its not get receives by the webapi controller method . its hitting the method instance perfectly . – Ramseen Ramsi May 11 '17 at 08:56
  • i have done like this , `var dataJSON = base64data;` and in ajax data `$.ajax({ type: 'POST', url: '', data: JSON.stringify(dataJSON), contentType: 'application/json; charset=utf-8', dataType: 'json' });` its working fine for me ... thank you for your help :) – Ramseen Ramsi May 11 '17 at 10:18