What is the problem with my ajax Post or with code in my views.py file that the audio is not being retrieved

拥有回忆 提交于 2021-02-11 13:38:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!