我试图以附件形式返回图像,但是在make_response()调用期间出现错误
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
get API的定义定义为:
class FileDownload(Files):
def get(self):
...
headers = {}
headers['Content-Type'] = file.mimetype
headers['Content-Disposition'] = 'attachment;filename='+file.name
return {'bucket': bucket, 'file': s3path}, 200, headers
以下是text / html的定义:
@api.representation('text/html')
def output_image(data, code, headers=None):
...
obj = s3.Object(bucket, data['file'])
data = obj.get()
body = data['Body'].read()
fileobj = io.BytesIO(body)
resp = make_response(fileobj.read())
resp.headers.extend(headers or {})
return resp
api.add_resource(FileDownload, '/download')