Hi everyone, I am going to share the code sample to validate and upload the image files using kendo ui with jQuery. The code details as give below.
Table of Content
1. The cshtml razor view code.
2. jQuery and JavaScript code
3. MVC 5 controller code
The razor view
<div class="form-group">
<label for="bio" class="control-label col-sm-2">Kendo ui Upload image file</label>
<div class="col-sm-10">
<input name="files" id="files" type="file" />
</div>
</div>
jQuery and JavaScript
<script type="text/javascript">
$(function () {
//This
used for check and validate image file.
var onSelect = function (evn) {
if (evn !== undefined && evn !== null) {
$.each(evn.files, function (key, val) {
if (val !== undefined) {
filepath = val.name;
var isValid = val.extension == ".JPG"
||
val.extension == ".JPEG"
||
val.extension == ".PNG"
||
val.extension == ".jpg"
||
val.extension == ".jpeg"
||
val.extension == ".png";
if (!isValid) {
evn.preventDefault();
showMSG($("#msg"), 'Please select only image files.');
}
}
});
}
};
var onComplete = function () {
$(".k-widget.k-upload").find("ul").remove();
$(".k-upload-files.k-reset").find("li").remove();
};
$("#files").kendoUpload({
async: {
saveUrl: '@Url.Action("Submit", "UserProfile")',
autoUpload: true
},
select: onSelect,
complete : onComplete
});
});
</script>