I'm using Flask and a few HTML-CSS-JS templates for an application which involves audio recordings.
I send a blob in a dedicated template (here index.html) as following :
<script type="text/javascript">
// some stuff with a new MediaRecorder() after clicking on a button, and then :
mediaRecorder.onstop = function(e) {
var blob = new Blob(chunks, { 'type' : mime }),
xmlhttp = new XMLHttpRequest(),
url = document.URL;
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", mime);
xmlhttp.send(blob);
chunks=[];
}
</script>
Then, the related part in views.py :
from flask import Flask, render_template, url_for, request, Response
@app.route('/index', methods=["GET","POST"])
def index():
count_records=-1
if request.method == 'POST':
count_records+=1
f_name = "audio/test"+str(count_records)+".webm"
blob = request.data
with open(f_name, 'ab') as f:
f.write(blob)
f.close()
I wanted to use count_records variable in order to save a different file after each request.method==["POST"]. It worked perfectly for one file, but not more. After that, got this message on Python console after recording new audio :
File 'audio/test0.webm' already exists. Overwrite ? [y/N] Not overwriting - exiting
So in fact the first request.method is always valid, and I didn't figure out how to manage that... Is this posible ?
EDIT
I want use one and only one view to achieve this (from /index to /index), not pass a variable from an other similar view (from /a to /b)
The user starts/stopped every MediaRecorder, he is involved in the process
As you see, the question supposed answer to the question not answers as well...
EDIT 2
A timestamp with datetime.datetime.now() fixed the problem. Not a Flask session as the bad-called answering post...