如何解决XmlHttpRequest请求元素为None
我正在使用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,v.videoWidth,v.videoHeight,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,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。此外,此方法重复三遍,然后停止发送反馈。我的意思是,control4函数可以打印3次(我认为应该在关闭相机之前都可以使用)。
有人对这个问题有什么想法吗?
谢谢,最好。
解决方法
您必须在request.FILES
中查找图片
def control4(request):
print(request.FILES['image'])
print(request.POST.get('threshold'))
return render(request,'local.html')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。