Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter file kendo-ui upload?

I'm using kendo-ui upload, I want to filter file (only allow choose .jpg, .png), but I don't know to implement in javascript, please help me!

1- .cshtml file

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

2- JavaScript

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

        async: {
            saveUrl: "Home/Save"
        }
    });
});
like image 429
PeaceInMind Avatar asked Dec 14 '22 21:12

PeaceInMind


2 Answers

To filter file, implement as below:

<input name="files" id="files" type="file" accept=".jpg,.png"/>
like image 171
PeaceInMind Avatar answered Dec 17 '22 11:12

PeaceInMind


Specify a 'select' event handler when you initialise your kendo upload widget:

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

Then use this to handle the file select event:

        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");
                }
        }
like image 31
Chris Halcrow Avatar answered Dec 17 '22 10:12

Chris Halcrow