views:

52

answers:

1

Hi all,

I'm new to WSGI on python; but have a windows server that's got isapi_wsgi installed on it. I also have a script that handles my GET requests all up and running great. The thing is, someone sends me a request, and I need to return a zip file to the requester. The following code is in my GET handler and it works, but doesn't seem like the right way to return the zipfile:

  # open zip file return it
  fin = open(zOutFilename, "rb")
  start_response( "200 OK", [('Content-Type', 'application/zip')])
  return fin.read()

The thing is, you're returning a 'stream' - which means you lose the filename (the browser simply names it the name of the GET query) and it seems awfully slow.

Is there a better way to return a file for download with wsgi then this method?

Thanks in advance

+3  A: 

Taken directly from PEP 333:

if 'wsgi.file_wrapper' in environ:
    return environ['wsgi.file_wrapper'](filelike, block_size)
else:
    return iter(lambda: filelike.read(block_size), '')

Also you probably want the Content-Disposition header for providing the file name to the client.

Aaron Gallagher
Yes yes - this is what I was looking for. It fixed my problem:<p> fin = open(zOutFilename, "rb") size = os.path.getsize(zOutFilename) start_response( "200 OK", [ ('Content-Type', 'application/zip'), ('Content-length', str(size)), ('Content-Disposition', 'attachment; filename='+finalModelName+'.zip') ] ) # return the entire file if 'wsgi.file_wrapper' in environ: return environ['wsgi.file_wrapper'](fin, 1024) else: return iter(lambda: fin.read(1024), '')</p>
madigan