Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix UnicodeError for filenames containing non-ASCII characters #1

Open
wants to merge 1 commit into from

1 participant

@nyuhuhuu

If filename contains non-ASCII characters, Django will raise UnicodeEncodeError 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.

@nyuhuhuu nyuhuhuu If filename contains non-ASCII characters, Django will raise UnicodeE…
…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.
e4a7151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 21, 2011
  1. @nyuhuhuu

    If filename contains non-ASCII characters, Django will raise UnicodeE…

    nyuhuhuu authored
    …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.
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 2 deletions.
  1. +3 −2 private_files/views.py
View
5 private_files/views.py 100755 → 100644
@@ -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
@@ -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
Something went wrong with that request. Please try again.