FileField cannot save file with chinese filenames.

32 views
Skip to first unread message

夏恺

unread,
Apr 1, 2009, 5:12:55 AM4/1/09
to django...@googlegroups.com
Hi all!

I wrote a model to hold files uploaded by users, it goes like this:

Model:

class Userfiles(models.Model):
user = models.ForeignKey(
User,
verbose_name="user name",
)
upload_file = models.FileField(
u"file",
upload_to='user/%Y/%m/%d',
)
upload_date = models.DateTimeField(
auto_now=True,
)

and when I try to add an mp3 file with chinese filename through the
admin interface, the files are not saved correctly, their names are
changed to something like: '.mp3', '_.mp3', '__.mp3'.

Has anyone any idea on how to solve this problem? Thanks!

Karen Tracey

unread,
Apr 1, 2009, 10:39:24 AM4/1/09
to django...@googlegroups.com
On Wed, Apr 1, 2009 at 5:12 AM, 夏恺 <xia...@gmail.com> wrote:

Hi all!

I wrote a model to hold files uploaded by users, it goes like this:

[snip]

and when I try to add an mp3 file with chinese filename through the
admin interface, the files are not saved correctly, their names are
changed to something like: '.mp3', '_.mp3', '__.mp3'.

Has anyone any idea on how to solve this problem? Thanks!

This is ticket #10254: http://code.djangoproject.com/ticket/10254

It has a patch you can try.  I'm waiting on feedback as to whether there is good reason to maintain the very restrictive set of allowed characters in file names before checking that patch in.  I don't see the reason for it, but it is code that has been in Django since forever so there may be some reason for it that I don't understand.

Karen

夏恺

unread,
Apr 1, 2009, 3:58:04 PM4/1/09
to django...@googlegroups.com
Karen Tracey 写道:

> On Wed, Apr 1, 2009 at 5:12 AM, 夏恺 <xia...@gmail.com
> <mailto:xia...@gmail.com>> wrote:
>
>
> Hi all!
>
> I wrote a model to hold files uploaded by users, it goes like this:
>
>
> This is ticket #10254: http://code.djangoproject.com/ticket/10254
>
> It has a patch you can try. I'm waiting on feedback as to whether
> there is good reason to maintain the very restrictive set of allowed
> characters in file names before checking that patch in. I don't see
> the reason for it, but it is code that has been in Django since
> forever so there may be some reason for it that I don't understand.
>
> Karen
>
Thanks Karen!

Yeah, I cannot understand why they will substitute all those character
out there too. Anyway, I removed that line from get_valid_filename in
django/utils/text.py and my app is happily running now. However, I got
that hack from this ticket:

http://code.djangoproject.com/ticket/6009

which, I believe, is more instructive.

Thank you again for your help!

Reply all
Reply to author
Forward
0 new messages