无法从输入标签中选择文件数组值

时间:2017-05-05 13:40:30

标签: javascript jquery

我试图找到用户是否上传了任何图片..

以下是代码

<input type="file"  name="profile_gallery[]">
<div id='submiform'>submit</div>

我先尝试了两种方法

    $(function(){
      $('#submitform').on('click', function(){
         alert($('input[name=profile_gallery]').val());
      });
    });

,错误是

    alert($('input[name=profile_gallery]').val());  //show undefined after and before selecting the file

第二个

    $(function(){
      $('#submitform').on('click', function(){
            var profile_gallery = ($('input[name=profile_gallery]'))[0].files;  
            alert(profile_gallery.length);
      });
    });

,错误是

    alert(profile_gallery.length);  //Uncaught TypeError: Cannot read property 'files' of undefined

1 个答案:

答案 0 :(得分:2)

  

这可以使用FileAPI

来实现

有几个选项

  • File.name :返回File对象引用的文件的名称。
  • File.size :返回文件的大小。
  • File.type :返回文件的MIME类型。

并且更多go here