将图像Blob从Ajax上传到Django [英] Upload an image blob from Ajax to Django

查看:609
本文介绍了将图像Blob从Ajax上传到Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ajax将图像上传到Django.

I am trying to to upload an image to Django using Ajax.

Ajax代码:

function sendImage() {
    console.log(blob);
    var fd = new FormData();
    fd.append('image', blob);
    var tet = $.ajax({
        url: '/login/photoverify/',
        type: 'POST',
        data: fd,
        async: false,
        contentType: false,
        processData: false,
        success: function (response) {
            console.log(response.driverBeacon);
            document.getElementById('username').setAttribute('value', response.username);
        },
        error: function (error) {
            console.log(error);
        }
    }).responseText;
}

Django代码:

def imageVerify(request):
    if request.method == 'POST':
        log.info('Inside imageVerify')
        myform = forms.Form(request.POST, request.FILES)
        log.info("Done loading form")
        if myform.is_valid():
            log.info(myform.cleaned_data)
            file = myform.cleaned_data['image']

但是myform.cleaned_data为空.有人可以帮忙吗?

But myform.cleaned_data is empty. Can someone please help?

推荐答案

您尚未将request.FILES传递给表单实例化.

You haven't passed request.FILES to the form instantiation.

myform = forms.Form(request.POST, request.FILES)

您可能还需要在$.ajax()调用中设置contentType: 'multipart/form-data'.

You might also need to set contentType: 'multipart/form-data' in the $.ajax() call.

这篇关于将图像Blob从Ajax上传到Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆