我正在尝试提供 base64 编码的图像文件并且失败了。要么我得到 UTF-8 编码的响应,要么return response
以一种有趣的方式得到线路错误。大多数我尝试过的所有东西都可以看作是下面摘录中的注释代码。追溯的详细信息如下。
我的问题是:如何返回 base64 编码文件?
#import base64
#with open(sPath, "rb") as image_file:
#encoded_string = base64.b64encode(image_file.read())
dContentTypes = {
'bmp' : 'image/bmp',
'cod' : 'image/cis-cod',
'git' : 'image/gif',
'ief' : 'image/ief',
'jpe' : 'image/jpeg',
.....
}
sContentType = dContentTypes[sExt]
response = FileResponse(
sPath,
request=request,
content_type= sContentType#+';base64',
#content_encoding = 'base_64'
#content_encoding = encoded_string
)
return response
取消注释该行#content_encoding = encoded_string
会给我错误:
AssertionError: Header value b'/9j/4AAQSkZJRgABAQAA' is not a string in ('Content-Encoding', b'/9j/4AAQSkZJRgABAQAA....')