Opened 7 years ago

Closed 7 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: UI/UX:


# -*- coding: utf-8 -*-
# in
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, 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 Changed 7 years ago by flyinflash

  • milestone set to 1.2
  • Needs documentation set
  • Needs tests unset
  • Owner changed from nobody to flyinflash
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 7 years ago by flyinflash

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 7 years ago by flyinflash

  • Version changed from 1.1 to 1.2-alpha

comment:4 Changed 7 years ago by peterbe

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 Changed 7 years ago by ubernostrum

  • milestone 1.2 deleted

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

comment:6 Changed 7 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from assigned to closed

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