您可以使用django的HTTP请求-响应模块来实现。
请参阅:https://docs.djangoproject.com/en/2.2/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment
如果您想像代码那样在生产中执行此操作,则可能还需要处理文件不可用的情况,设置错误并返回整洁的代码。另外,最好将其放在一个通用文件中,然后定义如下的download_file:
from common_library import FileResponse
from django.http import JsonResponse
def download_file( self ):
if self.error_response:
response = JsonResponse( { "error" : self.error_response } )
else:
response = FileResponse(self.report_file_abs_path,self.report_filename)
response['Content-Type'] = 'application/xlsx'
response['Content-Disposition'] = 'attachment; filename=' + self.report_filename
return response
注意:FileResponse是您可以定义的用户定义的包装函数。
本文链接:https://www.f2er.com/3146117.html