Kendo Upload upgrade(MVC) - OnUploadSelect code change -
with older version of telerik, had snippet of code find number of childnodes given below
function onuploadselect(ev) { var numberoffiles; if (ev.target.childnodes[1] != undefined && ev.target.childnodes[1] != null) { numberoffiles = ev.target.childnodes[1].childnodes.length; } if ((numberoffiles + ev.files.length) > 4) { //some custom validation error msgs being thrown } }
the basic logic of code prevent uploading more 4 files, ex - select 2 files,dont click on upload instead select file again , click on upload, i'm good(2+1<4) kendo uplaod, ev.target undefined, can suggest possible alternative this?
thanks adarsh
please try below code snippet.
kendo-html
<!doctype html> <html> <head> <title>test</title> <link href="http://cdn.kendostatic.com/2014.1.318/styles/kendo.common.min.css" rel="stylesheet" /> <link href="http://cdn.kendostatic.com/2014.1.318/styles/kendo.default.min.css" rel="stylesheet" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://cdn.kendostatic.com/2014.1.318/js/kendo.all.min.js"></script> </head> <body> <div class="demo-section"> <input name="files" id="files" type="file" /> </div> <script> $(document).ready(function() { $("#files").kendoupload({ select: onselect }); }); function onselect(e) { if (e.files.length > 4) { alert("please select max 4 files."); e.preventdefault(); } else { var existingfilecount = $(".demo-section li").length; if((e.files.length + existingfilecount) > 4) { alert("you can not upload more 4 files"); e.preventdefault(); } } } </script> </body> </html>
kendo-mvc
javascript
<script> function onselect(e) { if (e.files.length > 4) { alert("please select max 4 files."); e.preventdefault(); } else { var existingfilecount = $(".demo-section li").length; if((e.files.length + existingfilecount) > 4) { alert("you can not upload more 4 files"); e.preventdefault(); } } } </script>
view.cshtml
<div class="demo-section"> @(html.kendo().upload() .name("files") .events(events => events.select("onselect")) ) </div>
note : have used 'demo-section' class simplyfy code. if want rename class rename class in html/cshtml , javascript.
let me know if concern.
Comments
Post a Comment