2

选择多个文件时如何获取每个文件数据?

<input name="file" type="file" multiple="" >

<script type="text/javascript">
$(document).ready(function(){
  $(':file').change(function(){
    var file = this.files[0];
    name = file.name.toLowerCase();
    size = file.size;
    type = file.type;
  });
});
</script>
4

2 回答 2

6

this.files将产生一个数组。所以就这样循环...

$(document).ready(function(){
  $(':file').change(function(){
    for(var i=0; i< this.files.length; i++){
       var file = this.files[i];
       name = file.name.toLowerCase();
       size = file.size;
       type = file.type;
    }
  });
});
于 2013-05-14T14:46:14.660 回答
4

你可以试试这个——

$(':file').change(function(){
    var fileArray = this.files;
    $.each(fileArray,function(i,v){
      name = v.name.toLowerCase();
      size = v.size;
      type = v.type;
      alert(name);
    })
});
于 2013-05-14T14:48:56.020 回答