def file_download(request, id, fid):
file_obj = models.FileRepository.objects.filter(id=fid, project=request.tracer.project).first()
res = requests.get(
url=file_obj.file_path,
)
data = res.content
response = HttpResponse(data)
response['Content-Disposition'] = f'attachment; filename={file_obj.name}'
return response
1.首先获取到该文件的二进制数据:
data = request.get(url=file_obj.file_path).content
2.然后设置响应头并返回,浏览器会自动识别相应头的格式并判断
response = HttpResponse(data)
response['Content-Disposition'] = f'attachment; filename={file_obj.name}'
return response