Created
October 23, 2014 03:45
-
-
Save anhlt/936568bcfbc2c5da00eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from django.http import HttpResponse,StreamingHttpResponse | |
import requests | |
# Create your views here. | |
def download(request): | |
# NOTE the stream=True parameter | |
response = StreamingHttpResponse(generate_file(),content_type=' audio/mpeg') | |
return response | |
def generate_file(): | |
url = 'http://data23.chiasenhac.com/downloads/1236/4/1235116-a758ebfb/320/Time%20In%20A%20Bottle%20-%20Jim%20Croce%20%5BMP3%20320kbps%5D.mp3' | |
local_filename = url.split('/')[-1] | |
r = requests.get(url, stream=True) | |
with open(local_filename, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: # filter out keep-alive new chunks | |
yield chunk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment