Advertisement
Guest User

Untitled

a guest
May 1st, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. @login_required
  2. def download_file(request):
  3.     filename = request.GET.get('filename', '')
  4.     env_path = os.path.abspath(os.path.dirname(__file__))
  5.     path = '/'.join([env_path, '..', '{0}'.format(filename)])
  6.     try:
  7.         f = open(path, 'rb')
  8.     except IOError:
  9.         return HttpResponse('download failed')
  10.     else:
  11.         with f:
  12.             wrapper = FileWrapper(f)
  13.             response = HttpResponse(wrapper, content_type='application')
  14.             response['Content-Disposition'] = 'attachment; filename='+filename
  15.             return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement