how to check if a file is selected using javascript?

后端 未结 3 1300
情深已故
情深已故 2021-01-18 05:11

In php this is how you would check if a file is selected:

$_FILES[\'item\'][\'size\']>0

what about in JavaScript?

I need to know

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 05:42

    So lets say you have some html form and you have a custom file upload input:

     
    
    

    And you want to check the filename of the file the user has chosen/selected:

    Using jquery

    
    

    @https://stackoverflow.com/a/5670938/2979938

    For those using requireJS:

    $("input:file").change(function () {
      var fileName = $(this).val();
      alert(fileName); //Do with the filename whatever you want
    });
    

提交回复
热议问题