Skip to content

Instantly share code, notes, and snippets.

@joshourisman
Created October 15, 2009 18:08
Show Gist options
  • Save joshourisman/211149 to your computer and use it in GitHub Desktop.
Save joshourisman/211149 to your computer and use it in GitHub Desktop.
from django.http import HttpResponse
from piston.resource import Resource
class FileResource(Resource):
def __init__(self, handler, authentication=None, filename=None):
if filename:
self.filename = filename
else:
self.filename = "export.txt"
super(FileResource, self).__init__(handler, authentication=authentication)
def __call__(self, request, *args, **kwargs):
response = super(FileResource, self).__call__(request, *args, **kwargs)
if isinstance(response, HttpResponse):
response['Content-Disposition'] = 'attachment; filename=%s' % self.filename
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment