How to check if input file is empty in jQuery
Just check the length of files property, which is a FileList object contained on the input element
if( document.getElementById("videoUploadFile").files.length == 0 ){
console.log("no files selected");
}
Here is the jQuery version of it:
if ($('#videoUploadFile').get(0).files.length === 0) {
console.log("No files selected.");
}
To check whether the input file is empty or not
by using the file length property, index
should be specified like the following:
var vidFileLength = $("#videoUploadFile")[0].files.length;
if(vidFileLength === 0){
alert("No file selected.");
}
I know I'm late to the party but I thought I'd add what I ended up using for this - which is to simply check if the file upload input does not contain a truthy value with the not operator & JQuery like so:
if (!$('#videoUploadFile').val()) {
alert('Please Upload File');
}
Note that if this is in a form, you may also want to wrap it with the following handler to prevent the form from submitting:
$(document).on("click", ":submit", function (e) {
if (!$('#videoUploadFile').val()) {
e.preventDefault();
alert('Please Upload File');
}
}