Opened 16 years ago
Closed 16 years ago
#11994 closed (fixed)
save upload file with specify encode
| Reported by: | 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 , 16 years ago
| milestone: | → 1.2 |
|---|---|
| Needs documentation: | set |
| Owner: | changed from to |
| Status: | new → assigned |
comment:2 by , 16 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 16 years ago
| Version: | 1.1 → 1.2-alpha |
|---|
comment:4 by , 16 years ago
comment:5 by , 16 years ago
| milestone: | 1.2 |
|---|
1.2 is feature-frozen, moving this feature request off the milestone.
comment:6 by , 16 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
I suspect this is fixed by r12661, which restores old behavior of passing unicode file names to the OS for saving.
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
filenameis a Unicode string and not a byte string so perhaps the ticker reporter got aUnicodeDecodeErrorin his implementation ofget_path()and not inside Django.I was able to write a
FileFieldthat 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.