Skip to content

Commit

Permalink
If filename contains non-ASCII characters, Django will raise UnicodeE…
Browse files Browse the repository at this point in the history
…ncodeError exception when setting the X-SendFile header, because HTTP headers must be ASCII only.


A workaround for this problem is to pass the filename through Django's `smart_str()` function to convert it to a bytestream.
  • Loading branch information
woochica committed Jul 21, 2011
1 parent 0e2feab commit e4a7151
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions private_files/views.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils.http import http_date, parse_http_date
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import smart_str

from private_files.signals import pre_download

Expand Down Expand Up @@ -67,8 +68,8 @@ def _handle_xsendfile(request, instance, field_name):
response = HttpResponse()
response['Content-Type'] = mimetype
if field_file.attachment:
response['Content-Disposition'] = 'attachment; filename=%s'%basename
response["X-Sendfile"] = field_file.path
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(basename)
response["X-Sendfile"] = smart_str(field_file.path)
response['Content-Length'] = statobj.st_size
return response

Expand Down

0 comments on commit e4a7151

Please sign in to comment.