I am currently implementing a web application and I want the users to record some audio and then I want a submit button to POST the mp3 file recorded to the server.
My server (Flask)'s main route '/'
is waiting for the POST request:
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == "GET":
return render_template('index.html', request="GET")
else:
print request.files
print request.form
print request.form['file']
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
handle_file(file)
return render_template('index.html', request="POST")
Here is my JS code:
There are two main issues here:
1) When I download the mp3 file after recording, it cannot be opened by a media player. It seems like I am doing something wrong in just recording the audio.
2) When I print request.form
in my server after getting the POST request, I only get this:
ImmutableMultiDict([('file', u'')])
And print request.form['file']
returns an empty line.
Why is this happening? Is there something wrong with the POST request.
Finally, I want to be able to decode the string I am posting to convert back to mp3. How do I do that?
Note: None of this has to stay the same. The task is to record audio and then POST it to the server. If there is a more efficient way to do that, any tips are welcome. Also, I don't care if the file will be wav or mp3.