实现文件的下载
2019-08-21 本文已影响0人
不咸的Yan
report = CustomHistoryReport.objects.filter(id=reportid)
if report:
abs_path = REPORT_PATH + report[0].name + '.xlsx'
if not os.path.isfile(abs_path): # 判断下载文件是否存在
return Response(status=404)
else:
with open(abs_path, 'rb') as report:
file_content = report.read()
res = file_content
response = HttpResponse()
#声明流数据的类型
response['Content-Type'] = 'application/octet-stream'
#将流数据写入到相应对象中
response.write(res)
return response