简体   繁体   English

如何将JPEG二进制数据保存到Django ImageField

[英]How to Save JPEG binary data to Django ImageField

Goal: Get users signature through browser web application and save it to Django ImageField 目标:通过浏览器Web应用程序获取用户签名并将其保存到Django ImageField

I'm using https://github.com/szimek/signature_pad to get users signature on client side, then first saving it within a Django ModelForm TextField using toDataURL() . 我正在使用https://github.com/szimek/signature_pad在客户端获得用户签名,然后首先使用toDataURL()将其保存在Django ModelForm TextField中。

Now I'm stuck at the last step of converting the data into a jpeg image file and saving it to my Django ImageField. 现在,我停留在将数据转换为jpeg图像文件并将其保存到我的Django ImageField的最后一步。

I followed the tip at Uploading JavaScript generated image to Django but it only covers till decoding 64 bit string into 32 bit and I can't complete the last step: 我按照“ 将JavaScript生成的图像上传到Django”中的提示进行操作但是直到将64位字符串解码为32位为止,我都无法完成最后一步:

...and now ImageData contains the binary data, you can simply save to a file.. ...现在ImageData包含二进制数据,您只需将其保存到文件即可。

HTML HTML

{{ attendeeMegaForm.signatureHolder }}
<div id="signature-pad" class="m-signature-pad">
    <div class="m-signature-pad--body">
        <canvas></canvas>
    </div>
    <div class="m-signature-pad--footer text-center">
        <div class="description"><strong>Please Sign above <i class="fa fa-arrow-up"></i></strong></div>
         <button type="button" class="btn btn-sm btn-default clear" data-action="clear">Clear</button>
    </div>
</div>

JS JS

signaturePad = new SignaturePad(canvas, {
    backgroundColor: "rgb(255,255,255)",
    minWidth: 0.4,
    maxWidth: 2,
    dotSize: 1.2,
    onEnd: function () {
        signature = signaturePad.toDataURL("image/jpeg");
        document.getElementById('id_signatureHolder').value = signature;
    }
  });

Python 蟒蛇

if request.POST.get('updateAttendeeKey'):
    if attendeeMegaForm.is_valid():
        dataUrlPattern = re.compile('data:image/(png|jpeg);base64,(.*)$')
        ImageData = request.POST.get('signatureHolder')
        ImageData = dataUrlPattern.match(ImageData).group(2)


        if ImageData == None or len(ImageData) != 0:
            ImageData = base64.b64decode(ImageData)

Thanks! 谢谢!

Here is how I got it to work with signature_pad on the python 3.5/Django 1.9.2 backend: 这是我如何在python 3.5 / Django 1.9.2后端上使用signature_pad的:

import re, io
from base64 import decodestring
from django.core.files import File

data_url_pattern = re.compile('data:image/(png|jpeg);base64,(.*)$')
signature_url = request.POST.get("sig_data_url")
signature_data = data_url_pattern.match(signature_url).group(2)
signature_data = bytes(signature_data, 'UTF-8')
signature_data = decodestring(signature_data)
img_io = io.BytesIO(signature_data)
model_instance.image_field.save(filename, File(img_io))

Basically after you decode the string, just create the io object that will be used to create the Django File object. 基本上,在解码字符串之后,只需创建将用于创建Django File对象的io对象。 Once you have that, you can save it to the image field with the filename you want to use. 一旦有了它,就可以使用要使用的文件名将其保存到image字段。

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

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