Opened 15 years ago

Closed 15 years ago

#11994 closed (fixed)

save upload file with specify encode

Reported by: flyinflash@… Owned by: flyinflash
Component: File uploads/storage Version: 1.2-alpha
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

# -*- coding: utf-8 -*-
# in models.py
def get_path(instance, filename):
    path = osp.join('uploads', '中文'.encode('utf8'), filename)
    return path
class Files(models.Model):
    name = models.CharField(max_length=64)
    files = models.FileField(upload_to=get_path, storage=MyStorage)

Upload file with above code on Chinese version MS-Windows, it will get strange filename which contains unreadable characters.

I guess this problem associated with Chinese version MS-Windows using GBK encoding by default.
After read debug a long time and has read http://docs.djangoproject.com/en/dev/howto/custom-file-storage/, I think use custom storage which could specify save file with specify encode, such as GBK could fix my problem.

Django is a very very very good Web framework, I hope developers could add this feature, and please don't try to restrict user or down stream developers, thanks.

Change History (6)

comment:1 by flyinflash, 15 years ago

milestone: 1.2
Needs documentation: set
Owner: changed from nobody to flyinflash
Status: newassigned

comment:2 by flyinflash, 15 years ago

Triage Stage: UnreviewedAccepted

comment:3 by flyinflash, 15 years ago

Version: 1.11.2-alpha

comment:4 by Peter Bengtsson, 15 years ago

Unable to reproduce this.

In pure Python, if you try to do something like this:

os.path.join('uploads', '中文'.encode('utf8'), filename)

You'll get an UnicodeDecodeError error if filename is a Unicode string and not a byte string so perhaps the ticker reporter got a UnicodeDecodeError in his implementation of get_path() and not inside Django.

I was able to write a FileField that looked something like this:

def get_path(instance, filename):
    return os.path.join(u'中文'.encode('utf8'), filename.encode('utf8'))
class Thing(models.Model):
    name = models.CharField(max_length=64)
    thing_file = models.FileField(upload_to=get_path)

That worked fine. I also tried with my own storage class. A very basic one that just stores the file in /tmp and it worked perfectly fine. In other words could not reproduce any bugs in Django.

comment:5 by James Bennett, 15 years ago

milestone: 1.2

1.2 is feature-frozen, moving this feature request off the milestone.

comment:6 by Karen Tracey, 15 years ago

Resolution: fixed
Status: assignedclosed

I suspect this is fixed by r12661, which restores old behavior of passing unicode file names to the OS for saving.

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