我有一个灵活构建的自定义内容类型。在架构中(架构如下所列),我使用 'plone.namedfile.field.NamedFile' 用于附件/上传。
我想限制上传,以便只有 mp3 文件可以附加到我的内容类型。实现这一目标的最佳方法是什么?
这是我的内容类型的完整架构/模型:
<model xmlns="http://namespaces.plone.org/supermodel/schema">
<schema>
<field name="date" type="zope.schema.Date">
<description />
<title>Date</title>
</field>
<field name="speaker" type="zope.schema.TextLine">
<description />
<title>Speaker</title>
</field>
<field name="service" type="zope.schema.Choice">
<description />
<title>Service</title>
<values>
<element>1st Service</element>
<element>2nd Service</element>
</values>
</field>
<field name="audio_file" type="plone.namedfile.field.NamedFile">
<description />
<title>Audio File</title>
</field>
</schema>
</model>
我将从这里开始我的搜索:http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors
我决定使用 javascript 进行第一行验证。
我的解决方案基于以下位置找到的信息 通过扩展名限制可选择的文件 https://stackoverflow.com/questions/5796537/input-type-file-limit-selectable-files-by-extensions
根据建议,我的脚本看起来像这样:
$(document).ready( function() {
function checkFile(event) {
var fileElement = document.getElementById("form-widgets-audio_file-input");
var fileExtension = "";
if (fileElement.value.lastIndexOf(".") > 0) {
fileExtension = fileElement.value.substring(fileElement.value.lastIndexOf(".") + 1, fileElement.value.length);
}
if (fileExtension == "mp3") {
return true;
}
else {
alert("You must select a mp3 file for upload");
return false;
}
}
$("form#form").bind("submit",checkFile);
});
这是解决方案的一半,接下来我需要在服务器端添加验证。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)