So I’m trying to make a website that record your voice, the problem is that when I send to a flask server the blob file or the blob url, my flask python code says that is no content while it is, how can I send the blob, so the server can save it as a file.
JavaScript
x
19
19
1
mediaRecorder.addEventListener("stop", () => {
2
const audioBlob = new Blob(audioChunks, { type: "audio/wav" })
3
const audioUrl = URL.createObjectURL(audioBlob);
4
const audio = new Audio(audioUrl);
5
audio.play();
6
7
8
var data = new FormData()
9
data.append('file', audioUrl)
10
11
fetch('http://127.0.0.1:5000/receive', {
12
method: 'POST',
13
body: data
14
15
}).then(response => response.json()
16
).then(json => {
17
console.log(json)
18
});
19
and my python flask code:
JavaScript
1
14
14
1
@app.route("/receive", methods=['post'])
2
def form():
3
files = request.files
4
file = files.get('file')
5
print(file)
6
7
with open(os.path.abspath(f'backend/audios/{file}'), 'wb') as f:
8
f.write(file.content)
9
10
response = jsonify("File received and saved!")
11
response.headers.add('Access-Control-Allow-Origin', '*')
12
13
return response
14
is there a way to do it? send record blob file, download it into python?
Advertisement
Answer
The problem is in this line:
JavaScript
1
2
1
data.append('file', audioUrl)
2
you don’t use FormData.append the right way. it should be:
JavaScript
1
2
1
data.append('file', audioBlob , 'file')
2
See documentation: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append