python - XmlHttpRequest 请求元素为 None

标签 python django xmlhttprequest webrtc blob

我正在使用 Django 并尝试使用 XMLHttpRequest 将网络摄像头数据发送到后台 (view.py) 以处理每个帧。我关注this link大多数人都针对我的问题提出了类似的方法。

$(document).ready(function(){
  $('#trigger_button').click(function(){
    navigator.mediaDevices.getUserMedia(constraints)
    .then(stream => {
      document.getElementById("myVideo").srcObject = stream;
    })
    .catch(err => {
      alert('navigator.getUserMedia error: ', err)
    });
    drawCanvas.width = v.videoWidth;
    drawCanvas.height = v.videoHeight;
    imageCanvas.width = uploadWidth;
    imageCanvas.height = uploadWidth * (v.videoHeight / v.videoWidth);
    drawCtx.lineWidth = 4;
    drawCtx.strokeStyle = "cyan";
    drawCtx.font = "20px Verdana";
    drawCtx.fillStyle = "cyan";
    imageCanvas.getContext("2d").drawImage(v, 0, 0, v.videoWidth, v.videoHeight, 0, 0, uploadWidth, uploadWidth * (v.videoHeight / v.videoWidth));

    imageCanvas.toBlob(postFile, 'image/jpeg');
  });
});
function postFile(file) {
  var formdata = new FormData();
  formdata.append("image", file);
  formdata.append("threshold", scoreThreshold);
  var xhr = new XMLHttpRequest();
  xhr.open('POST', apiServer, true);

  xhr.onload = function () {
    if (this.status === 200) {
      var objects = JSON.parse(this.response);
      drawBoxes(objects);
      imageCtx.drawImage(v, 0, 0, v.videoWidth, v.videoHeight, 0, 0, uploadWidth, uploadWidth * (v.videoHeight / v.videoWidth));
      imageCanvas.toBlob(postFile, 'image/jpeg');
    }
    else {
      alert(this.status);
    }
  };
  xhr.send(formdata);
}

然后,我尝试访问 view.py 中请求中的数据,如下所示:

def control4(request):
    print(request.POST.get('image'))
    print(request.POST.get('threshold'))
    return render(request, 'local.html')

但是,虽然request.Post.get('threshold')返回一个值,但request.POST.get('image')返回None。另外,该方法重复3次后就停止发送反馈。我的意思是,control4 函数打印 3 次(我认为它应该一直工作到相机关闭为止)。

有人知道问题出在哪里吗?

谢谢你,最好的。

最佳答案

您必须在 request.FILES 中查找图像

def control4(request):
    print(request.FILES['image'])
    print(request.POST.get('threshold'))
    return render(request, 'local.html')

关于python - XmlHttpRequest 请求元素为 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64272668/

相关文章:

python - Django:当我尝试删除用户时出现 AttributeError

python - Django:NoReverseMatch 位于 ' ' 。 ' ' 不是有效的 View 函数或模式名称

python - Django从get方法获取数据并传递给post方法

Angular 5 混合屏蔽内容

javascript - JSON 用户搜索

php - 'interface' 在 python 中是否像在 PHP 中一样实现

python - 在 JupyterLab 中以编程方式运行单元

python - Google Cloud VM 在部署时创建新版本

python - Django 注册 redux 仅使用电子邮件注册(无密码)

javascript - 是否可以配置 IE 以允许 XHR 到 `http://127.0.0.1 from` HTTPS?