Django 从文件名中删除“@”和其他特殊字符
Django removes "@" and other special character from file name
我想上传一个文件,文件名是上传者的邮箱。我知道这不是 OS 文件系统问题,也不是不受支持的 python 操作,因为我在 python 中确实以这种格式保存了图像。现在我想将图像保存在模型中(特别是 ImageField)。
如果存在同名图像,我将使用 OverwriteStorage 覆盖图像。
正在保存图像
model = Model.objects.create(email=email)
# Blob to image
tempfile_io = io.BytesIO()
img.save(tempfile_io, 'JPEG')
image_file = InMemoryUploadedFile(tempfile_io, None, email + ".jpeg",'image/jpeg',tempfile_io.getbuffer().nbytes, None)
print("email is", email)
model.pic.save(email + ".jpg",image_file)
型号
class Model(models.Model):
email = models.EmailField()
pic = models.ImageField(upload_to="pics", storage=OverwriteStorage())
覆盖存储
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, *args, **kwargs):
print("name is", name)
if self.exists(name):
self.delete(name)
return name
但是由于某些原因,我在 OverwriteStorage-get_available_name 中没有得到确切的名字。
email is xyz@example.com
name is xyzexamle.com.jpg
注意 @ 符号是如何被粗暴地删除的。
是否有任何我需要禁用的文件名字符串检查。如何让 Django 使用给定的确切文件名(只要可能)?
您必须将 get_valid_name(name)
替换为 documented
get_valid_name(name)
Returns a filename suitable for use with the underlying storage
system. The name argument passed to this method is either the original
filename sent to the server or, if upload_to is a callable, the
filename returned by that method after any path information is
removed. Override this to customize how non-standard characters are
converted to safe filenames.
来自source
def get_valid_name(self, name):
"""
Return a filename, based on the provided filename, that's suitable for
use in the target storage system.
"""
return get_valid_filename(name)
我想上传一个文件,文件名是上传者的邮箱。我知道这不是 OS 文件系统问题,也不是不受支持的 python 操作,因为我在 python 中确实以这种格式保存了图像。现在我想将图像保存在模型中(特别是 ImageField)。
如果存在同名图像,我将使用 OverwriteStorage 覆盖图像。
正在保存图像
model = Model.objects.create(email=email)
# Blob to image
tempfile_io = io.BytesIO()
img.save(tempfile_io, 'JPEG')
image_file = InMemoryUploadedFile(tempfile_io, None, email + ".jpeg",'image/jpeg',tempfile_io.getbuffer().nbytes, None)
print("email is", email)
model.pic.save(email + ".jpg",image_file)
型号
class Model(models.Model):
email = models.EmailField()
pic = models.ImageField(upload_to="pics", storage=OverwriteStorage())
覆盖存储
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, *args, **kwargs):
print("name is", name)
if self.exists(name):
self.delete(name)
return name
但是由于某些原因,我在 OverwriteStorage-get_available_name 中没有得到确切的名字。
email is xyz@example.com
name is xyzexamle.com.jpg
注意 @ 符号是如何被粗暴地删除的。 是否有任何我需要禁用的文件名字符串检查。如何让 Django 使用给定的确切文件名(只要可能)?
您必须将 get_valid_name(name)
替换为 documented
get_valid_name(name)
Returns a filename suitable for use with the underlying storage system. The name argument passed to this method is either the original filename sent to the server or, if upload_to is a callable, the filename returned by that method after any path information is removed. Override this to customize how non-standard characters are converted to safe filenames.
来自source
def get_valid_name(self, name):
"""
Return a filename, based on the provided filename, that's suitable for
use in the target storage system.
"""
return get_valid_filename(name)