Django(音频)文件验证
我正在试验一个允许用户上传音频文件的网站。我已经阅读了所有我能拿到的文档,但找不到太多关于验证文件的内容。
这里完全是新手(之前从未做过任何类型的文件验证)并试图弄清楚这一点。有人可以握住我的手并告诉我我需要知道什么吗?
一如既往,预先感谢您。
I'm experimenting with a site that will allow users to upload audio files. I've read every doc that I can get my hands on but can't find much about validating files.
Total newb here (never done any file validation of any kind before) and trying to figure this out. Can someone hold my hand and tell me what I need to know?
As always, thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望在将文件写入磁盘之前对其进行验证。当您上传文件时,表单将被验证,然后上传的文件将被传递到处理程序/方法,该处理程序/方法处理对服务器上磁盘的实际写入。因此,在这两个操作之间,您想要执行一些自定义验证以确保它是有效的音频文件
您可以:
(我还没有测试过这段代码)
models.py
forms.py
views.py
from utils import handle_uploaded_file
utils.py
https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#file-uploads">https:// /docs.djangoproject.com/en/dev/topics/http/file-uploads/#file-uploads
https://docs.djangoproject.com/en/dev/ref/forms/fields/#filefield
https://docs.djangoproject.com/en/dev/ref /files/file/#django.core.files.File
You want to validate the file before it gets written to disk. When you upload a file, the form gets validated then the uploaded file gets passed to a handler/method that deals with the actual writing to the disk on your server. So in between these two operations, you want to perform some custom validation to make sure it's a valid audio file
You could:
(I haven't tested this code)
models.py
forms.py
views.py
from utils import handle_uploaded_file
utils.py
https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#file-uploads
https://docs.djangoproject.com/en/dev/ref/forms/fields/#filefield
https://docs.djangoproject.com/en/dev/ref/files/file/#django.core.files.File