Opened 15 years ago

Closed 15 years ago

#11638 closed (duplicate)

HttpResponseRedirect to a file causes UnicodeEncodeError

Reported by: Alexander Owned by: nobody
Component: HTTP handling Version: 1.1
Severity: Keywords: HttpResponseRedirect file UnicodeEncodeError us-ascii utf8
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Karen Tracey)

I'm trying to download via GET a file with name in russian and getting

Environment:

Request Method: GET
Request URL: http://172.16.23.33/file/4719e0bdedaa4f741f032991894d52ecb08c3476a598504fd1fee92d
Django Version: 1.1
Python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'filez.filezupload',
 'debug_toolbar']
Installed Middleware:
('firepy.django.middleware.FirePHPMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in __call__
  78.             return self.view_func(request, *args, **kwargs)
File "/var/www/filez/filezupload/views.py" in send_file
  259.         return HttpResponseRedirect(path)
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in __init__
  407.         self['Location'] = redirect_to
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in __setitem__
  320.         header, value = self._convert_to_ascii(header, value)
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in _convert_to_ascii
  309.                     value = value.encode('us-ascii')

Exception Type: UnicodeEncodeError at /file/4719e0bdedaa4f741f032991894d52ecb08c3476a598504fd1fee92d
Exception Value: ('ascii', u'/site_media/upload/alecs/\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u043e\u0447\u0442\u044b.doc', 25, 34, 'ordinal not in range(128)')

The code which cause this error:

upfile = UpFile.objects.get(file_hash=file_hash)
path = os.path.join(settings.UPLOAD_ROOT, request.user.username,
upfile.file_name)
return HttpResponseRedirect(path)

If filename in English everything works ok.
The file was uploaded this way:

dir_path = os.path.join(settings.UPLOAD_ROOT, request.user.username)
upfile = UpFile.objects.create(user=user, file_name=file.name,
file_hash=file_hash,
file_content_type=file.content_type,file_path=file_path,
file_url=file_url, file_size=file.size)
path = '%s/%s' % (dir_path, file.name)
destination = open(path.encode('utf8'), 'wb+')
for chunk in file.chunks():
    destination.write(chunk)
destination.close() 
DEFAULT_CHARSET = 'utf-8'
DATABASE_ENGINE = 'sqlite3' 

The name of the file is correctly readable in my system(ls -lh):
locale

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL= 

Change History (1)

comment:1 by Karen Tracey, 15 years ago

Description: modified (diff)
Resolution: duplicate
Status: newclosed

Fixed formatting -- please use preview.

The description for #11522 seems to say the fix proposed there would also fix this case, so I'm going to close this as a dup of that one.

Note: See TracTickets for help on using tickets.
Back to Top