如何在jQuery中检查输入文件是否为空

时间:2022-11-13 22:55:45

Brand new to JS.
I am trying to check if the file input element is empty when submitting the form with jQuery/JavaScript. I have gone through a bunch of solutions and nothing is working for me. I am trying to avoid the /c/fakepath (unless there is no other option)

全新的JS。当使用jQuery/JavaScript提交表单时,我尝试检查文件输入元素是否为空。我已经解决了很多问题,但没有什么对我有用。我试图避免/c/fakepath(除非没有其他选择)

<input type="file" name="videoFile" id="videoUploadFile" />

This does not work:

这并不工作:

var vidFile = $("#videoUploadFile").value;

The only way I can get the filename is if I use the following:

我唯一能得到文件名的方法是:

var vidFile = document.getElementById("videoUploadFile").files[0].name;

If there is no file available the code throws an error:

如果没有可用的文件,代码会抛出一个错误:

cannot read property name of undefined

无法读取未定义的属性名

which makes sense because the array is not set. but I cannot figure out how to do any error handling with this.

这是有意义的,因为数组不是set,但是我不知道如何处理这个。

How do I properly grab the file input element videoUploadFile, check if it's empty, throw an error message if it's empty?

如何正确抓取文件输入元素videoUploadFile,检查文件是否为空,如果为空则抛出错误信息?

4 个解决方案

#1


99  

Just check the length of files property, which is a FileList object contained on the input element

只需检查file的长度属性,它是一个包含在输入元素中的FileList对象

if( document.getElementById("videoUploadFile").files.length == 0 ){
    console.log("no files selected");
}

#2


80  

Here is the jQuery version of it:

以下是jQuery版本:

if ($('#videoUploadFile').get(0).files.length === 0) {
    console.log("No files selected.");
}

#3


7  

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.");
}

#4


-1  

Questions : how to check File is empty or not?

问题:如何检查文件是否为空?

Ans: I have slove this issue using this Jquery code

答:我用Jquery代码解决了这个问题

//If your file Is Empty :     
      if (jQuery('#videoUploadFile').val() == '') {
                       $('#message').html("Please Attach File");
                   }else {
                            alert('not work');
                   }

    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="videoUploadFile">
<br>
<br>
<div id="message"></div>

#1


99  

Just check the length of files property, which is a FileList object contained on the input element

只需检查file的长度属性,它是一个包含在输入元素中的FileList对象

if( document.getElementById("videoUploadFile").files.length == 0 ){
    console.log("no files selected");
}

#2


80  

Here is the jQuery version of it:

以下是jQuery版本:

if ($('#videoUploadFile').get(0).files.length === 0) {
    console.log("No files selected.");
}

#3


7  

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.");
}

#4


-1  

Questions : how to check File is empty or not?

问题:如何检查文件是否为空?

Ans: I have slove this issue using this Jquery code

答:我用Jquery代码解决了这个问题

//If your file Is Empty :     
      if (jQuery('#videoUploadFile').val() == '') {
                       $('#message').html("Please Attach File");
                   }else {
                            alert('not work');
                   }

    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="videoUploadFile">
<br>
<br>
<div id="message"></div>