Jquery input.files equivalent

前端 未结 4 742
一向
一向 2020-12-09 16:46

I have a simple html form with a single file upload input. (jsfiddle)

In the past, I have accessed the file selected by the user using input.files, howe

相关标签:
4条回答
  • 2020-12-09 17:12

    You have to access the element. Try:

    file = $("#fileInput")[0].files[0];
    alert(file); //C
    

    or (thank you Jack)

    file = $("#fileInput").prop('files')[0];
    alert(file);
    

    Fiddle

    0 讨论(0)
  • 2020-12-09 17:13
    $(function () {
        $("#cmdSubmit").bind("click", function () {
           alert(this.files);
         });        
    });​
    
    0 讨论(0)
  • 2020-12-09 17:17

    jQuery doesn't provide a wrapper for the Files API. So there isn't a jQuery style way to do this, at least not built into the jQuery core.

    Your options:

    • stick with option A
    • wait for jQuery to include such a wrapper (they might not)
    • find an extension written by someone else (if one exists)
    • write an extension yourself
    0 讨论(0)
  • 2020-12-09 17:25

    Try the below code

    var file = $("#fileInput").get(0).files[0];
     alert(file);
    

    Thanks

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