aleksandra-tarkowska/openmicroscopy — comment on commit 22723d3d77813033592d6286a04f2be8ea8bb081
File:  line 0
By aleksandra-tarkowska:

This is the standard way of sending back files in django:

from django.core.files.temp import NamedTemporaryFile
def send_file(request):
newfile = NamedTemporaryFile()
...
wrapper = FileWrapper(newfile)
response = HttpResponse(wrapper, content_type=mime_type)
response['Content-Disposition'] = 'attachment; filename=%s' % newfile.name
response['Content-Length'] = newfile.tell()
return response

NamedTemporaryFile should be deleted once the newfile object is evicted. When you step out of the send_file function, there should be no more reference to the newfile object, but it's not guaranteed that the object will be garbage-collected and its del method called. From the other hand I am not aware about any built-in mechanism to automatically delete TemporaryFile instances within HttpRespons.

I got ride of some of NamedTemporaryFile becuse we are `yielding` OriginalFile and this can write directly to HttpResponse, but there are still two cases where: multiple files are zipped and mrc map is asking for real file.