Restrict upload by filetype or mimetype using Dexterity on Plone

微笑、不失礼 提交于 2019-12-22 09:25:17

问题


I have a custom content type, built with dexterity. In the schema (The schema is listed below), I use 'plone.namedfile.field.NamedFile' for attachements/uploads.

I would like to restrict uploads so that only mp3 files can be attached to my content type. What is the best approach for achieving this?

Here is the full schema/model for my content type:

<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>

I shall begin my search here: http://plone.org/products/dexterity/documentation/manual/developer-manual/reference/default-value-validator-adaptors


回答1:


I've decided to use javascript for my first line of validation. I've based my solution on information found at <input type="file"> limit selectable files by extensions

Based on the advice my script looks something like this:

$(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);

});

This is half the solution, next I'll need to add validation on the server side.



来源:https://stackoverflow.com/questions/14145087/restrict-upload-by-filetype-or-mimetype-using-dexterity-on-plone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!