Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created February 10, 2012 16:55
Show Gist options
  • Save alejandrobernardis/1790864 to your computer and use it in GitHub Desktop.
Save alejandrobernardis/1790864 to your computer and use it in GitHub Desktop.
Tornado, Download File
class AdminFileHandler(BaseHandler):
@authenticated
def get(self, file_name):
_file_dir = os.path.abspath("")+"/my/path/downloads"
_file_path = "%s/%s" % (_file_dir, file_name)
if not file_name or not os.path.exists(_file_path):
raise HTTPError(404)
self.set_header('Content-Type', 'application/force-download')
self.set_header('Content-Disposition', 'attachment; filename=%s' % file_name)
with open(_file_path, "rb") as f:
try:
while True:
_buffer = f.read(4096)
if _buffer:
self.write(_buffer)
else:
f.close()
self.finish()
return
except:
raise HTTPError(404)
raise HTTPError(500)
@osullivj
Copy link

Thanks - worked nicely for me...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment