Record user's voice with JS and post to django server

I want to record a voice from the user and post it to the server side (django). I don't want save the voice with models, but I want to convert the voice to text with Python libraries.

For this, I use this plugin, but my the recorded voice is stored in blob format and in an audio tag. I can't send a blob to the server. I also used Recorder.js, but it is also saved as a blob.

you can use the fetch API or an AJAX request to send the audio data as a binary file. Before sending the data, you need to convert the audio data (blob) to a format that can be easily transmitted, such as base64 or array buffer

// Convert the blob to a base64 string
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
  var base64Data = reader.result;
  // Send the base64 encoded data to the server using fetch
  fetch('/your-django-endpoint/', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      data: base64Data
    })
  })
  .then(response => response.json())
  .then(data => {
    console.log('Success:', data);
  })
  .catch((error) => {
    console.error('Error:', error);
  });
};

in your Django view, you can receive the base64 encoded data, decode it, and convert it back to an audio file. Then, you can use the Python libraries to convert the audio to text

Back to Top