简体   繁体   English

如何限制 Kendo UI Web 上传只允许一次上传?

[英]How do I limit Kendo UI Web Upload To allow only a single upload?

I am currently using Kendo UI for uploading files to a DB Using MVC3 and Razor and Entity Framework.我目前正在使用 Kendo UI 使用 MVC3 和 Razor 和实体框架将文件上传到数据库。 I have it working great in several areas of my site, except when I need to restrict it to allowing only a singular upload.我让它在我网站的几个区域运行良好,除非我需要将它限制为只允许单个上传。 I have multiple set to false, which I need to disallow multiple selections, but the user is still allowed to click the select button any number of times to add files, violating the requirements for this field in the DB.我将 multiple 设置为 false,我需要禁止多选,但仍然允许用户多次单击 select 按钮添加文件,这违反了数据库中此字段的要求。

I tried some suggestions I thought I found on their site, but they are referring to the current selected items sent in the current request, not the whole of the uploads list (see image below).我尝试了一些我认为在他们的网站上找到的建议,但他们指的是当前请求中发送的当前选定项目,而不是整个上传列表(见下图)。

<script type="text/javascript">
  function singleFile(e) {
    var files = e.files;
    if (e.files.length > 1) {
      alert('Only one file may be uploaded, cancelling operation...');
      e.preventDefault();
    }
  }
</script>
@(Html.Kendo().Upload()
  .Name("resumeAttachments")
  .Multiple(false)
  .Async(async => async
      .Save("ResumeSave", "File")
  )
  .Events(c => c
      .Upload("resumeOnUpload")
  )
  .Events(c => c
      .Success("resumeOnSuccess")
  )
  .Events(c => c
      .Complete("singleFile")
  )
)

文件列表 - 允许上传多个文件,单独

After I was given the requirement to prevent multiple uploads I stumbled across this page. 在我被要求阻止多次上传后,我偶然发现了这个页面。
"multiple" set to FALSE works just fine if it is done correctly. 设置为FALSE的“multiple”如果正确完成就可以正常工作。

(While you CAN use the Kendo Razor syntax, notice when you view the page source that the .Kendo() actually gets converted to .kendoUpload (虽然你可以使用剑道剃刀语法,注意当您查看.Kendo()实际上被转换到.kendoUpload页面的源代码

Thus I prefer this syntax in javascript (after the @using): 因此我更喜欢javascript中的这种语法 (在@using之后):

@using Kendo.Mvc.UI;

<script type="text/javascript">

$(document).ready(function() {
    $("#files").kendoUpload({"multiple":false,
        async: {
            saveUrl: '@Url.Action("Save", "Upload", new { typeOfUploadedFile= @Model.DocName.ToString(), @proposalNo = @Model.ProposalNo.ToString(),  area = ""})',
            removeUrl: '@Url.Action("Remove", "Upload")',
            autoUpload: true
        }
    });
});   

</script>

After a little bit of thinking over the weekend (and a long weekend of vacation to relax), it hit me... Changing the singleFile function to the following will disable the control after the file is uploaded. 经过一段时间的思考(以及度假放松的长周末),它击中了我......将singleFile函数更改为以下将在文件上传后禁用控件。

function singleFile(e) {
  var upload = $("#resumeAttachments").data("kendoUpload");

  // disables the upload after upload
  upload.disable();
}

Also, adding: 另外,添加:

var dropzone = $(".k-dropzone").addClass("hide");

to the singleFile() function will hide the select button after the upload is completed, giving the very real impression that you can no longer upload, given that: 单个文件()函数将在上传完成后隐藏选择按钮,给人以非常真实的印象,即您无法再上传,因为:

.hide {
  display: none; }

is defined in your css somewhere. 在你的css某处定义。

You must multiple property of kendo upload set value to false ; 您必须将kendo upload设置值的multiple属性设置为false ;
for example @(Html.Kendo().Upload().Multiple(false)) 例如@(Html.Kendo().Upload().Multiple(false))

I know this is a really old thread, however i just want to post the issues we have encounted as well. 我知道这是一个非常古老的主题,但我只是想发布我们已经解决的问题。 Adding Multiple with async upload would work with below simple code 使用异步上传添加多个将使用以下简单代码

$("#files").kendoUpload({
                        multiple : false,
                        async : {
                                saveUrl : FileUploadURL,
                                removeUrl : FileRemoveURL,
                                autoUpload : true
                                },
                        remove : onRemove,
                        success : onSuccess
                        });

however there will be a really annoying behaviour that, when users select another file when the previous selected file uploading is still in progress, then from the front page it looks like the previous selected file is removed, but the fact is the file uploading of previous selected file will still continue, which means the file will get uploaded to your server anyway and you have no chance to trigger the removeUrl to delete the unused file, and of course consuming addtional bandwidth. 然而,当用户在上一次选择的文件上传仍在进行时选择另一个文件时,会出现一种非常烦人的行为,然后从首页看起来像先前选择的文件被删除,但实际上是以前的文件上传选定的文件仍将继续,这意味着文件无论如何都会上传到您的服务器,您无法触发removeUrl删除未使用的文件,当然也会消耗额外的带宽。

what we have done so far to get around this issue is to add a small handling in the onRemove event handler, which will invoking the clearFileByUid to stop the uploading. 到目前为止我们解决这个问题的方法是在onRemove事件处理程序中添加一个小处理,它将调用clearFileByUid来停止上传。

function onRemove(e) {
        for(var removedFileId of getFileId(e)){
            //All in progress file should be stopped!
            var fileEntry=$('.k-file-progress[' + kendo.attr('uid') + '="' + removedFileId + '"]', this.wrapper)
            if(fileEntry!=null&&fileEntry.length>0){this.clearFileByUid(removedFileId);}
        }

}
function getFileId(e) {
        return $.map(e.files, function(file) {
                var fileId = file.uid;
                return fileId;
                });
}

Restrict by setting multiple false in your script document-ready method.通过在脚本文档就绪方法中设置多个 false 来限制。

$("#kendoSingleFileInput").kendoUpload({
                multiple: false,
                select: onSelect
  });

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

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