问题类似于这个django rest framework return file
尝试应用类似的解决方案在Django Rest ApiView
中返回可执行的python二进制文件:
from wsgiref.util import FileWrapper
bin_file = open(f'cli/builds/dist/cli', 'rb')
response = Response(FileWrapper(bin_file), content_type='application/octet-stream')
response['Content-Disposition'] = f'attachment; filename="cli"'
response.status_code = status.HTTP_200_OK
return response
得到Object of type 'FileWrapper' is not JSON serializable
错误。参考前面提到的SO主题 - 这个解决方案适用于zip文件。
问题 - 为什么它不能用于我的设置,返回python可执行文件?
python 3.6.5
,djangorestframework==3.8.2
尝试过Response
和HttpResponse
课程
尝试使用HttpResponse
而不是DRF的Response
,
from wsgiref.util import FileWrapper
from django.http.response import HttpResponse
bin_file = open(f'cli/builds/dist/cli', 'rb')
response = HttpResponse(FileWrapper(bin_file), content_type='application/octet-stream')
response['Content-Disposition'] = f'attachment; filename="cli"'
response.status_code = status.HTTP_200_OK
return response