我在 Django 中使用 FileField 编写了一个电子邮件表单类。我想通过检查其 mimetype 来检查上传文件的类型。随后,我想将文件类型限制为 pdf、word 和打开的 Office 文档。
为此,我已经安装了 python-magic,并希望按照 python-magic 的规范检查文件类型:
mime = magic.Magic(mime=True)
file_mime_type = mime.from_file('address/of/file.txt')
但是,最近上传的文件在我的服务器上缺少地址。我也不知道 mime 对象有任何类似于“from_file_content”的方法来检查给定文件内容的 mime 类型。
在Django表单中使用magic来验证上传文件的文件类型的有效方法是什么?
I have written an email form class in Django with a FileField. I want to check the uploaded file for its type via checking its mimetype. Subsequently, I want to limit file types to pdfs, word, and open office documents.
To this end, I have installed python-magic and would like to check file types as follows per the specs for python-magic:
mime = magic.Magic(mime=True)
file_mime_type = mime.from_file('address/of/file.txt')
However, recently uploaded files lack addresses on my server. I also do not know of any method of the mime object akin to "from_file_content" that checks for the mime type given the content of the file.
What is an effective way to use magic to verify file types of uploaded files in Django forms?
发布评论
评论(5)
斯坦描述了带有缓冲液的良好变体。不幸的是,这种方法的弱点是将文件读取到内存中。另一种选择是使用临时存储的文件:
另外,您可能想检查文件大小:
另外:
所以你可能想要像 so 一样处理它:
另外,你可能想要
seek(0)
在read()
之后:上传数据的存储位置
Stan described good variant with buffer. Unfortunately the weakness of this method is reading file to the memory. Another option is using temporary stored file:
Also, you might want to check the file size:
Additionally:
So you might want to handle it like so:
Also, you might want to
seek(0)
afterread()
:Where uploaded data is stored
为什么不尝试在您看来类似的事情:
或者使用
request.FILES
代替form.cleaned_data
如果django.forms.Form
确实是不是一个选择。Why no trying something like that in your view :
Or use
request.FILES
in place ofform.cleaned_data
ifdjango.forms.Form
is really not an option.另外,您可能想要
seek
:read()
之后的 (0)来自 Django 代码。在验证期间对图像字段执行。
Also, you might want to
seek(0)
afterread()
:Example from Django code. Performed for image fields during validation.
您可以使用 django-safe-filefield 包来验证上传的文件扩展名是否与 MIME 匹配-类型。
You can use django-safe-filefield package to validate that uploaded file extension match it MIME-type.
如果您正在处理文件上传并且只关心图像
Django 将为您设置
content_type
(或者更确切地说为它自己?):它不依赖于用户提供的内容类型。但
django.db.models.fields.files.FieldFile.file
是一个未记录属性。
实际上,最初
content_type
是从 请求,但是当表单经过验证,值已更新。
关于非图像,做
request.FILES['name'].read()
对我来说似乎没问题。首先,这就是 Django 所做的。二、默认大于2.5Mb的文件
存储在 磁盘。因此,让我向您指出其他答案
这里。
出于好奇,这里是导致更新的堆栈跟踪
content_type
:django.forms.forms.BaseForm.is_valid:self.errors
django.forms.forms.BaseForm.errors: 自我。 full_clean()
django.forms.forms.BaseForm.full_clean: 自我。 _clean_fields()
django.forms.forms.BaseForm._clean_fiels: 字段。干净()
django.forms.fields.FileField.clean: 超级( ).clean()
django.forms.fields.Field.clean: 自我。 to_python()
django.forms.fields.ImageField.to_python< /a>
In case you're handling a file upload and concerned only about images,
Django will set
content_type
for you (or rather for itself?):It doesn't rely on content type provided by the user. But
django.db.models.fields.files.FieldFile.file
is an undocumentedproperty.
Actually, initially
content_type
is set from the request, but whenthe form gets validated, the value is updated.
Regarding non-images, doing
request.FILES['name'].read()
seems okay to me.First, that's what Django does. Second, files larger than 2.5 Mb by default
are stored on a disk. So let me point you at the other answer
here.
For the curious, here's the stack trace that leads to updating
content_type
:django.forms.forms.BaseForm.is_valid: self.errors
django.forms.forms.BaseForm.errors: self.full_clean()
django.forms.forms.BaseForm.full_clean: self._clean_fields()
django.forms.forms.BaseForm._clean_fiels: field.clean()
django.forms.fields.FileField.clean: super().clean()
django.forms.fields.Field.clean: self.to_python()
django.forms.fields.ImageField.to_python