1
我有我發送到路線views.py
和解碼回使用的代碼錯誤節省解碼圖像到模型的Django
image_data = request.POST.get('image_data')
pattern = r'^data:(?P<mime_type>[^;]+);base64,(?P<image>.+)$'
result = re.match(pattern, image_data)
if result:
mime_type = result.group('mime_type')
image = result.group('image').decode('base64')
saveImage = uploadImage(image=image, imageMime=mime_type)
saveImage.save()
一旦解碼的基礎-64串,我需要將其保存到項目中的文件夾而不是本地文件夾。但是,當我調用.save()時,出現編碼錯誤:DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte. You passed in '\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x
。
我需要剝離PNG部分才能使用嗎?
我的模型如下:
class uploadImage(models.Model):
image = models.ImageField(upload_to='Gallery')
imageMime = models.CharField(max_length=50)
我試過了,我得到這個錯誤:_io.BytesIO'對象沒有屬性'_committed' – user782400