首页 > 解决方案 > 从 Kendo Upload 中排除文件类型

问题描述

我正在尝试设置 Kendo 上传验证。我可以将其设置为只接受列出的文件,但我想翻转它。我希望能够指定不允许的文件类型。例如,如果我想排除.exe文件类型,我.exe在代码中指定。

下面是我目前拥有的代码,它指定了允许的文件类型白名单;但我想使用黑名单进行验证。

@(Html.Kendo().Upload()
  .Name("files")
  .TemplateId("fileTemplate")
  .HtmlAttributes(new { @class = "no-auto-save", AutoComplete = "off" })
    .Async(a => a.Save("AddAttachments", "Transaction").AutoUpload(false))
    .Events(e => e.Upload("uploadAttachments").Success("attachmentsUploaded"))
    .Validation(validation => validation.AllowedExtensions(new string[] { ".doc", ".docx", ".ppt", ".pptx", ".xls", ".xlsx", ".jpg", ".jpeg", ".png", ".html", ".gif", "txt", ".pdf", ".json", ".zip" }))
)

标签: asp.netkendo-ui

解决方案


为事件添加处理程序upload

组件声明

.Events(events => events
    .Upload("onUpload")
)

事件处理 JavaScript

function onUpload(e) {
  if (e.files[0].extension == ".exe") {
    e.preventDefault();
  }      
}

function onUpload(e) {
    // An array with information about the uploaded files
    var files = e.files;

    // Checks the extension of each file and aborts the upload if it is not .jpg
    $.each(files, function () {
        if (this.extension.toLowerCase() != ".jpg") {
            alert("Only .jpg files can be uploaded")
            e.preventDefault();
        }
    });

推荐阅读