Opened 15 years ago

Last modified 15 years ago

#11638 closed

HttpResponseRedirect to a file causes UnicodeEncodeError — at Initial Version

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

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

  1. response = callback(request, *callback_args, callback_kwargs)

File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in call

  1. return self.view_func(request, *args, kwargs)

File "/var/www/filez/filezupload/views.py" in send_file

  1. return HttpResponseRedirect(path)

File "/usr/local/lib/python2.6/dist-packages/django/http/init.py" in init

  1. selfLocation = redirect_to

File "/usr/local/lib/python2.6/dist-packages/django/http/init.py" in setitem

  1. header, value = self._convert_to_ascii(header, value)

File "/usr/local/lib/python2.6/dist-packages/django/http/init.py" in _convert_to_ascii

  1. 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 (0)

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