Created
December 4, 2014 20:38
-
-
Save codecats/08d2e1b4175dd5697457 to your computer and use it in GitHub Desktop.
django streaming file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get(self, request, *args, **kwargs): | |
# def data(): | |
# for i in xrange(10): | |
# csvwriter.writerow([i,"a","b","c"]) | |
# data = read_and_flush() | |
# yield data | |
def stream_response_generator(): | |
for x in range(1,11): | |
yield "%s\n" % x # Returns a chunk of the response to the browser | |
time.sleep(1) | |
def file_chunks_generator(file_object, chunk_size=1024): | |
while True: | |
data = file_object.read(chunk_size) | |
if not data: | |
break | |
yield data | |
print 'sleep' | |
time.sleep(1) | |
print 'yea' | |
with open('/home/t/Desktop/download.jpg') as f: | |
resp = HttpResponse(file_chunks_generator(f, 1024), content_type = "image/jpeg") | |
resp['Content-Disposition'] = 'filename=%s' % '123.pdf' | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment