2

I'm working on an audio web application that records the user's voice and then post as a .wav/.mp3 file to the server. I have some code to POST my audio blob in my .js file but I'm not sure if it is correct. I'm also not sure how to receive the blob audio and download it as a .wav file at the Python server side

Here's a snippet of my code in my app.js file

var formdata = new FormData();
formdata.append("audio", blob, "test.wav")

$.ajax(
{  
    type: 'POST',                  
    url: "http://localhost/pyserver.py",
    data: formdata,
    contentType: false,
    processData: false,
        
    success: function (data) {                       
        // data is what is sent back to you from the server, handle it here.
        console.log(data);
    },
    complete: function () {
        // let's say you have a "loading" window up, this is where you close it.
    },
    error: function (jqXHR, textStatus, errorThrown) {
        // handle error.
        console.log(errorThrown);
    }
});
Rony Macfly
  • 210
  • 2
  • 4
  • 10
Coder123
  • 21
  • 2
  • [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – charlietfl Jul 16 '20 at 00:05

1 Answers1

1

js file

function sendAudio(blob)
{
    var wavFile = new File([ blob ], "audio.wav");      
    var form    = new FormData();
    form.append("myAudio", wavFile);
    
    $.ajax(
    {
        url: "/getAudio/",
        type: "POST",
        data: form,
        contentType: false,
        processData: false,
        success: function(getData)
        {
            console.log(getData);
        }
    });
}

views.py

def getAudio(request):
    if request.method == "POST":
        if request.FILES.get("myAudio", False):
            handleUploadFile(request.FILES["myAudio"])
    return HttpResponse()

def handleUploadFile(f):  
    with open("myFolder/" + f.name, "wb+") as destination:  
        for chunk in f.chunks():  
            destination.write(chunk)

handleUploadFile is better placed in a separate "function.py"

Rony Macfly
  • 210
  • 2
  • 4
  • 10
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 21 '21 at 20:27