问题
I am currently working on a project in Django which records audio on the browser and send it to the server for communication. i have already recorded the audio but when i try to POST it using an ajax request, an empty dictionary is received in views.py.
here is my script where i have recorded the audio and tried to post an ajax request(record.html)
<form method="POST" enctype='multipart/form-data'>
{%csrf_token%}
<audio scr="" name="audio" id="audio1"></audio><br>
<input type="button" value="click me to record" id="recordbtn">
<input type="button" value="click me to stop record" id="stopbtn">
</form>
var blob;
navigator.mediaDevices.getUserMedia({audio:true})
.then(function success(stream){
var options;
try{
options={memiType:'audio/webm;codecs=opus'};
}
catch{
console.log('media type not supported')
}
mediaRecorder=new MediaRecorder(stream,options);
mediaRecorder.ondataavailable=DataAvailable;
recordbtn.onclick = function()
{
mediaRecorder.start();
console.log('recoding started');
}
var chunks=[];
function DataAvailable(event)
{
chunks.push(event.data);
console.log('dataprocessed');
var audio = document.getElementById('audio1');
audio.controls = true;
blob = new Blob(chunks , { 'type' : 'audio/webm; codecs=opus' });
console.log(blob);
chunks = [];
var audioURL = URL.createObjectURL(blob);
audio.src = audioURL;
console.log('src assigned');
var token = '{{csrf_token}}';
console.log(token);
var fd=new FormData();
fd.append('Blob1',blob);
console.log(fd);
console.log(fd.getAll('Blob1'));
$.ajaxSetup({headers:{ "X-CSRFToken": token}})
$.ajax(
{
url:"{%url 'record:voice2pg'%}",
type:'POST',
contentType:'audio/webm; codecs=opus',
data:fd,
processData:false,
success:function(){
alert('post successful')
}
}
)
}
stopbtn.onclick = function()
{
mediaRecorder.stop();
console.log('recording stopeed');
}
})
.catch(function failure()
{
console.log('failure occured');
}
);
views.py
from django.shortcuts import render
from django.http import HttpResponse
def voice2(request):
if request.method=='GET':
return render(request,'record.html')
else:
if request.method == 'POST' :
print(request.POST)
print(request.FILES)
audio=request.FILES.get("Blob1")
print(audio)
return render(request, 'record.html')
I am new for working with audio.In my console i could see the blob that is appended with the formdata, then where am wrong ?
the console screen of my browser this is what i see
output:
(typ) C:\Users\KRISHNA DAVE\project\tempproject> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
December 04, 2019 - 12:03:17
Django version 2.2.6, using settings 'tempproject.settings'
Starting ASGI/Channels version 2.3.1 development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
HTTP GET /voice2 200 [0.04, 127.0.0.1:49173]
<QueryDict: {}>
None
<MultiValueDict: {}>
None
HTTP POST /voice2 200 [0.01, 127.0.0.1:49173]
why an i receiving an empty dictionary? is there a problem with the code ajax request? Please help me with the problem because i am new for using audio
来源:https://stackoverflow.com/questions/59170410/what-is-the-problem-with-my-ajax-post-or-with-code-in-my-views-py-file-that-the