jQuery grab a file uploaded with input type='file'

后端 未结 2 593
醉梦人生
醉梦人生 2021-02-04 07:42

I want to grab the file uploaded in a tag.

When I do $(\'#inputId\').val(), it only grabs the name of the file, not the

2条回答
  •  清酒与你
    2021-02-04 08:06

    Use event.target.files for change event to retrieve the File instances.

    $('#inputId').change(function(e) {
      var files = e.target.files; 
    
      for (var i = 0, file; file = files[i]; i++) {
        console.log(file);
      }
    });
    

    Have a look here for more info: http://www.html5rocks.com/en/tutorials/file/dndfiles/

    This solution uses File API which is not supported by all browser - see http://caniuse.com/#feat=fileapi .

提交回复
热议问题