简体   繁体   English

django发布请求未在终端中显示javascript blob对象

[英]django post request is not showing javascript blob object in terminal

I've been banging my head on the wall for hours with this. 我已经在墙上敲了好几个小时。 I am trying to use recorder.js using the source from this example https://webaudiodemos.appspot.com/AudioRecorder/ 我正在尝试使用来自本示例https://webaudiodemos.appspot.com/AudioRecorder/的源代码的recorder.js

Now I want to modify it so that the user will hear the audio played back to them and then the y will have the chance to upload it to the server if they like it. 现在,我想对其进行修改,以使用户可以听到播放给他们的音频,然后y如果愿意,可以将其上载到服务器。 I got the audio to playback by adding the return of the method createObjectURL(blob) to an audio element. 通过将方法createObjectURL(blob)的返回值添加到音频元素,可以播放音频。 GREAT

Now I just have to write a post request and send it to my django instance to handle it in a view...which is where it gets weird. 现在,我只需要编写一个发布请求,并将其发送到我的django实例即可在视图中处理它……这很奇怪。

I used jquery to post an ajax request like this... 我用jQuery发布这样的ajax请求...

$(document).ready(function () {
  $("#submit_audio_file").click(function () {
    var data = new FormData();
    data.append("csrfmiddlewaretoken", $("[name=csrfmiddlewaretoken]").val());
    data.append("audio_file", blob, "test");
    // Display the key/value pairs
    for(var pair of data.entries()) {
       console.log(pair[0]+ ', '+ pair[1]); 
    }
    $.ajax({
      type: "POST",
      url: $("#post_url").val(),
      data: data,
      processData: false,  // prevent jQuery from converting the data
      contentType: false,  // prevent jquery from changing something else as well 
      success: function(response) {
        alert(response);
      }
    });
  })
});

when I make this request go through I see this in the console... 当我发出此请求时,我会在控制台中看到此信息...

csrfmiddlewaretoken, 3YBQrdOUkquRDD5dN0hTJcUXYVFiNpSe
audio_file, [object File]

and then in my django CBV I put a print request.POST.items() in the first line so I could see what was coming in. In my terminal I see this... 然后在我的django CBV中,在第一行中放置一个print request.POST.items() ,这样我就可以看到进来的东西。在我的终端中,我看到了这个...

 [(u'csrfmiddlewaretoken', u'mymiddlewaretokenvalue')]

there is no audio_file key in the post request at all. 发布请求中根本没有audio_file密钥。 Wy would it show in the javascript console and then disappear in the django request? 为什么会在javascript控制台中显示然后在django请求中消失?

I also see a potential problem in the future because I think that the javascript console is just printing a string of [object File] which will obviously not do what I want it to. 将来我还会看到一个潜在的问题,因为我认为javascript控制台只是在打印[object File]的字符串,这显然不会执行我想要的操作。

Any ideas of where to go next? 有什么下一步的想法吗?

As Daniel Roseman wrote, uploaded files end up in request.FILES . 正如Daniel Roseman所写,上载的文件最终位于request.FILES One more thing - from jQuery documentation: 还有一件事-来自jQuery文档:

As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. 从jQuery 1.6开始,您可以传递false来告诉jQuery不要设置任何内容类型标头。

and Django documentation states: 和Django文档规定:

Note that request.FILES will only contain data if the request method was POST and the form that posted the request has the attribute enctype="multipart/form-data". 请注意,仅当请求方法为POST并且发布请求的表单具有属性enctype =“ multipart / form-data”时,request.FILES仅包含数据。 Otherwise, request.FILES will be empty. 否则,request.FILES将为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM