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

后端 未结 2 599
醉梦人生
醉梦人生 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:00

    This is likely referring to HTML5 files property. See w3 and sample jsfiddle

    0 讨论(0)
  • 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 .

    0 讨论(0)
提交回复
热议问题