Sending audio data from JS to Python
You are passing an array of event objects to the Blob constructor not the audio data.
The data is e.data
, so your code should be
mediaRecorder.ondataavailable = e => { chunks.push(e.data);}
You can do it with the multipart file upload in javascript
export function uploadAudio(audioBlob) { let data = new FormData(); data.append('file', audioBlob); return axios .post(`http://localhost:3030/audiorecog`, data, { headers: { 'Content-Type': 'multipart/form-data', }, }) .then(res => { console.log(res) return res });}
In Python, you can receive the blob and save it
@app.route('/audiorecog', methods = ['GET', 'POST'])def audiorecog(): if request.method == 'POST': print("Recieved Audio File") file = request.files['file'] print('File from the POST request is: {}'.format(file)) with open("audio.wav", "wb") as aud: aud_stream = file.read() aud.write(video_stream) return "Success" return 'Call from get'
Have you tried using FormData?
var form = new FormData();form.append('file', BLOBDATA, FILENAME);$.ajax({ type: 'POST', url: 'ServerURL', data: form, // Our pretty new form cache: false, processData: false, // tell jQuery not to process the data contentType: false // tell jQuery not to set contentType}).done(function(data) { console.log(data);});
On the python end, you could check if the data's there by doing something like:
@app.route('/messages', methods=['POST'])def api_message(): app.logger.debug(request.files['file'].filename)