在将音频 blob 传输到 Django 服务器后,为什么数据库中保存的是 .336Z 文件而不是 wav 文件?

问题描述 投票:0回答:1

我正在开发一个使用 Recorder.js 和 Django 的录音应用程序。单击“上传”按钮时,音频 blob 将发送到 Django 服务器并保存在数据库中。但是,我面临一个问题,即音频文件不是以 wav 格式保存,而是在数据库中保存为 .336Z 文件。为什么不能保存为wav格式的文件?

app.js

function createDownloadLink(blob) {
    
    var url = URL.createObjectURL(blob);
    var au = document.createElement('audio');
    var li = document.createElement('li');
    var link = document.createElement('a');

    //name of .wav file to use during upload and download (without extendion)
    var filename = new Date().toISOString();

    //add controls to the <audio> element
    au.controls = true;
    au.src = url;

    //save to disk link
    link.href = url;
    link.download = filename+".wav"; //download forces the browser to donwload the file using the  filename
    link.innerHTML = "Save to disk";

    //add the new audio element to li
    li.appendChild(au);
    
    //add the filename to the li
    li.appendChild(document.createTextNode(filename+".wav "))

    //add the save to disk link to li
    li.appendChild(link);
    
    //upload link
    var upload = document.createElement('a');
    upload.href="#";
    upload.innerHTML = "Upload";
    upload.addEventListener("click", function(event){
          var xhr=new XMLHttpRequest();
          xhr.onload=function(e) {
              if(this.readyState === 4) {
                  console.log("Server returned: ",e.target.responseText);
              }
          };
          var fd=new FormData();
          fd.append("audio_data",blob, filename.wav);
          xhr.open("POST","/save-audio/",true);
          xhr.send(fd);
    })
    li.appendChild(document.createTextNode (" "))//add a space in between
    li.appendChild(upload)//add the upload link to li

    //add the li element to the ol
    recordingsList.appendChild(li);
}

模型.py

class Audio(models.Model):
    record= models.FileField(upload_to="media", null=True, blank=True)

views.py

@csrf_exempt
def save_audio(request):
    if request.method == 'POST':
        audio_file = request.FILES['audio_data']
        audio = Audio(record=audio_file)
        audio.save()
        return JsonResponse({'status': 'success'})
    else:
        return JsonResponse({'status': 'error'})
javascript python django blob
1个回答
0
投票

问题出在这一行

  fd.append("audio_data",blob, filename.wav);

改成

  fd.append("audio_data", blob, filename+".wav");

那应该有用

© www.soinside.com 2019 - 2024. All rights reserved.