欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 在Django中把Base64字符串保存为ImageField

在Django中把Base64字符串保存为ImageField

2025/6/25 11:19:11 来源:https://blog.csdn.net/lifanping/article/details/148798879  浏览:    关键词:在Django中把Base64字符串保存为ImageField

在数据model中使用ImageField来管理avatar。

class User(models.Model):AVATAR_COLORS = (('#212736', 'Black'),('#2161FD', 'Blue'),('#36B37E', 'Green'),('#F5121D', 'Red'),('#FE802F', 'Orange'),('#9254DE', 'Purple'),('#EB2F96', 'Magenta'),)def generate_filename(self, filename):url = "avatar-{}-{}".format(self.user.username, filename)return urlavatar = models.ImageField(upload_to=generate_filename, verbose_name='头像', null=True, blank=True)avatar_color = models.CharField(verbose_name='头像颜色', choices=AVATAR_COLORS, max_length=10, blank=True, null=True)#这里省略其他字段

前端avatar字段传输的是Base64字符串,Django后端将其转换为ContentFile后进行save。

首先,确保你已经安装了Pillow库,它是Django中处理图像的常用库。

import base64
import binascii
import imghdr
import io
import uuidfrom django.core.exceptions import ValidationError
from django.core.files.base import ContentFileclass Base64ToImageFile(object):"""A django-rest-framework field for handling image-uploads through raw post data.It uses base64 for en-/decoding the contents of the file."""ALLOWED_TYPES = ("jpeg","jpg","png","gif")EMPTY_VALUES = (None, '', [], (), {})INVALID_FILE_MESSAGE = "Please upload a valid image."INVALID_TYPE_MESSAGE = "The type of the image couldn't be determined."def to_file(self, base64_data):# Check if this is a base64 stringif base64_data in self.EMPTY_VALUES:return Noneif isinstance(base64_data, str):# Strip base64 header.if ';base64,' in base64_data:header, base64_data = base64_data.split(';base64,')# Try to decode the file. Return validation error if it fails.try:decoded_file = base64.b64decode(base64_data)except (TypeError, binascii.Error, ValueError):raise ValidationError(self.INVALID_FILE_MESSAGE)# Generate file name:file_name = self.get_file_name(decoded_file)# Get the file name extension:file_extension = self.get_file_extension(file_name, decoded_file)if file_extension not in self.ALLOWED_TYPES:raise ValidationError(self.INVALID_TYPE_MESSAGE)complete_file_name = file_name + "." + file_extensiondata = ContentFile(decoded_file, name=complete_file_name)return dataraise ValidationError('Invalid type. This is not an base64 string: {}'.format(type(base64_data)))def get_file_name(self, decoded_file):return str(uuid.uuid4())def get_file_extension(self, filename, decoded_file):try:from PIL import Imageexcept ImportError:raise ImportError("Pillow is not installed.")extension = imghdr.what(filename, decoded_file)# Try with PIL as fallback if format not detected due# to bug in imghdr https://bugs.python.org/issue16512if extension is None:try:image = Image.open(io.BytesIO(decoded_file))except (OSError, IOError):raise ValidationError(self.INVALID_FILE_MESSAGE)extension = image.format.lower()extension = "jpg" if extension == "jpeg" else extensionreturn extensiondef base64_string_to_file(base64_string):return Base64ToImageFile().to_file(base64_string)

在创建用户过程中,给avatar字段赋值ContentFile类型

        avatar_file = base64_string_to_file(avatar_string)if avatar_file:request_data["avatar"] = avatar_fileelse:request_data.pop('avatar', None)serializer = UserCreateSerializer(data=request_data)if serializer.is_valid():user = serializer.save()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词