I'm trying to get an audio file from my user's microphone, and when I try to send it over to my server to download it keeps erroring.
Here's my js code to get the audio
navigator.mediaDevices.getUserMedia({audio:true})
.then(stream => {handlerFunction(stream)})
function handlerFunction(stream) {
rec = new MediaRecorder(stream);
rec.ondataavailable = e => {
audioChunks.push(e.data);
if (rec.state == "inactive"){
let blob = new Blob(audioChunks,{type:'audio/mpeg-3'});
recordedAudio.src = URL.createObjectURL(blob);
recordedAudio.controls=true;
recordedAudio.autoplay=true;
var data = new FormData()
data.append('file', blob, 'file')
sendData(data)
}
}
}
function sendData(data) {
fetch('http://127.0.0.1:5000/test',{
method: 'POST',
body: data
})
}
and my python code:
if request.method == "POST":
files = request.files
file = files.get('file')
print(file.content)
return render_template('test.html')
and here's the trace:
File "C:\Users\Rohan\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Rohan\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Rohan\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
0, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Rohan\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "main.py", line 24, in test2
print(file.content)
File "C:\Users\Rohan\anaconda3\lib\site-packages\werkzeug\datastructures.py", line 3095, in __getattr__
return getattr(self.stream._file, name)
AttributeError: '_io.BytesIO' object has no attribute 'content'
CodePudding user response:
It seems your trying to send a file stream back to the client which I don't think is supported by flask. Use the documentation for future reference.
https://flask.palletsprojects.com/en/2.2.x/
also. Why isn't is not a question. try: file not being read after being sent.
CodePudding user response:
I solved my problem, all I needed to do was do file.read
and not file.content
.