Skip to content

Instantly share code, notes, and snippets.

@oldj
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldj/91d3b4e40229e2268437 to your computer and use it in GitHub Desktop.
Save oldj/91d3b4e40229e2268437 to your computer and use it in GitHub Desktop.
在Django中提供大内容(或大文件)下载
def bigFileView(request):
u"""在Django中提供大内容(或大文件)下载
@see http://oldj.net/article/django-big-file-response/
"""
# do something...
def readFile(fn, buf_size=262144):
f = open(fn, "rb")
while True:
c = f.read(buf_size)
if c:
yield c
else:
break
f.close()
file_name = "big_file.txt"
response = HttpResponse(readFile(file_name))
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment