在Django的文件字段中保存base64图像。

11 浏览
0 Comments

在Django的文件字段中保存base64图像。

我有以下输入:

"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA7YAAAISCAIAAAB3YsSDAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAA5JxJREFUeNrsnQl4FEX6xqcJJEAS7ivhBkMAQTSJ4h0QEQ+I90rAc1cOL3QBXXV1AV1dVwmrsCqQ9VwJ6HoC7oon0T8iEkABwRC5IeE+kkAIkPT/nfmSmprunskk5CDw/p55hu7qOr76api8........"

我想将这个文件保存在文件字段中。我该怎么做呢?

models.py

class SomeModel(models.Model):
    file = models.FileField(upload_to=get_upload_report)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

我正在尝试这样做:

def get_file(data):
    from django.core.files import File
    return File(data)

并将返回的文件保存到模型实例中:

somemodel.file = get_file(image_base64_data)

但是它会报以下错误:

AttributeError at /someurl/
'File' object has no attribute 'decode'

0