Opened 15 years ago
Closed 15 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 , 15 years ago
milestone: | → 1.2 |
---|---|
Needs documentation: | set |
Owner: | changed from | to
Status: | new → assigned |
comment:2 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 15 years ago
Version: | 1.1 → 1.2-alpha |
---|
comment:4 by , 15 years ago
comment:5 by , 15 years ago
milestone: | 1.2 |
---|
1.2 is feature-frozen, moving this feature request off the milestone.
comment:6 by , 15 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:
You'll get an UnicodeDecodeError error if
filename
is a Unicode string and not a byte string so perhaps the ticker reporter got aUnicodeDecodeError
in his implementation ofget_path()
and not inside Django.I was able to write a
FileField
that looked something like this: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.