繁体   English   中英

如何过滤文件kendo-ui上传?

[英]How to filter file kendo-ui upload?

我正在使用kendo-ui上传,我想过滤文件(只允许选择.jpg,.png),但我不知道在javascript中实现,请帮帮我!

1- .cshtml文件

<input name="files" id="files" type="file" />

2- JavaScript

$(document).ready(function () {
    $("#files").kendoUpload({
        multiple: false,

        async: {
            saveUrl: "Home/Save"
        }
    });
});

要过滤文件,请执行以下操作:

<input name="files" id="files" type="file" accept=".jpg,.png"/>

初始化kendo上传小部件时指定“select”事件处理程序:

$(document).ready(function () {
    $("#files").kendoUpload({
        multiple: false,
        async: {
            saveUrl: "Home/Save"
        },
        select: onSelect,
    });
});

然后使用它来处理文件选择事件:

        function onSelect(e) {
            var files = e.files
            var acceptedFiles = [".jpg", ".jpeg", ".png", ".gif"]
            var isAcceptedImageFormat = ($.inArray(files[0].extension, acceptedFiles)) != -1

            if (!isAcceptedImageFormat) {
                   e.preventDefault();
                   alert("Image must be jpeg, png or gif");
                }
        }

您必须使用OnSelect事件并限制您想要的计数。

http://docs.kendoui.c​​om/api/web/upload#select

http://demos.kendoui.c​​om/web/upload/events.html

function onSelect(e) {
    if (e.files.length > 1) {
        alert("Please select only 1 file.");
        e.preventDefault();
    }
}

在下面的输入文件选项中添加验证:

validation: {
 allowedExtensions: [".gif", ".jpg", ".png"]
}

如果您正在寻找更多信息,请查看此演示: https//demos.telerik.com/kendo-ui/upload/validation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM