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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25793880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:52:23  来源:igfitidea点击:

How to check if input file is empty in jQuery

javascriptjqueryhtml

提问by user3753569

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.

这是有道理的,因为未设置数组。但我无法弄清楚如何对此进行任何错误处理。

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

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

回答by Patrick Evans

Just check the length of filesproperty, which is a FileListobject contained on the input element

只需检查files属性的长度,它是包含在输入元素上的FileList对象

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

回答by Salim

Here is the jQuery version of it:

这是它的 jQuery 版本:

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

回答by Mamun

To check whether the input file is empty or not by using the file length property, indexshould be specified like the following:

要使用文件长度属性检查输入文件是否为空,index应指定如下:

var vidFileLength = $("#videoUploadFile")[0].files.length;
if(vidFileLength === 0){
    alert("No file selected.");
}

回答by Adnan Limdiwala

Questions : how to check File is empty or not?

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

Ans: I have slove this issue using this Jquery code

Ans:我使用这个 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>

回答by Joel Youngberg

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:

我知道我参加聚会迟到了,但我想我会添加我最终为此使用的内容 - 这是简单地检查文件上传输入是否不包含带有 not 运算符和 JQuery 的真实值,如下所示:

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');
  }

}