Skip to content

Instantly share code, notes, and snippets.

@anhlt
Created October 23, 2014 03:45
Show Gist options
  • Save anhlt/936568bcfbc2c5da00eb to your computer and use it in GitHub Desktop.
Save anhlt/936568bcfbc2c5da00eb to your computer and use it in GitHub Desktop.
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