Django休息框架ApiView重新编译可执行文件

问题描述 投票:1回答:1

问题类似于这个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.5djangorestframework==3.8.2

尝试过ResponseHttpResponse课程

python django python-3.x django-rest-framework
1个回答
1
投票

尝试使用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
© www.soinside.com 2019 - 2024. All rights reserved.