def file_download(request):
""" 下载文件 """
# 我这里是访问网络地址下载文件
# 下载本地文件的话直接打开文件下载就ok
# with open('xxx', mode='rb') as f:
# data = f.read()
res = requests.get(xxx)
# 文件分块处理(适用于大文件)
data = res.iter_content()
# 设置content_type='application/octet-stream' 用于提示下载框
response = HttpResponse(data, content_type='application/octet-stream')
# 设置响应头, 文件 & 文件名转义
response['Content-Disposition'] = 'attachment; filename={}'.format(escape_uri_path(file_object.name))
return response
正文完