Thanks giles.
Here's what I ended up doing. Works well. Can you see any holes?
On Django:
#urls.py
urlpatterns = [
...
url(r'^download/(?P<image_id>[0-9]+)/$', views.download, name='download'),
]
#views.py
def download(request, image_id):
image = get_object_or_404(StoredImage, pk=image_id)
path = os.path.join(settings.MEDIA_ROOT, image.image.name) #The image file is in my image model
fsock = open(path, 'rb')
response = HttpResponse(fsock, content_type='image')
response['Content-Disposition'] = "attachment; filename=%s" % (image.image.name)
return response
On Local (with django.setup() called):
from tempfile import NamedTemporaryFile
def download_image(image):
#Create download url for image
url = HOST_URL + reverse('myapp:download', args=[image.id])
#Get file extension
file_extension = os.path.splitext(image.image.name)[1]
#Request the image
r = requests.get(url, stream=True)
if r.status_code == 200:
#Save it to harddrive
image = NamedTemporaryFile(mode='wb', delete=False, suffix=file_extension)
for chunk in r:
image.write(chunk)
return image