我有一个项目需要将存储在模型的 FileField 中的文件转换为 Base64 格式,但我不知道该怎么做。
我尝试使用 open(model_instance.file_field.file)
,它不起作用
我尝试使用 base64.b64encode(model_instance.file_field.file)
,也不起作用
最佳答案
老话题,但只是在这里有一些解决方案。
import base64
encoded_base64 = base64.b64encode(model_instance.file_field.file.read()) # return bytes
encoded_str = encoded_base64.decode('utf-8') # return string
关于django - 如何在 Django 中使用 s3 到 base64 在 FileField 中编码文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49324010/