JS simple file upload instance code (no plug in required)

  • 2020-03-29 23:46:43
  • OfStack


<span class="up-btn" id="selectFile"> Please select file </span>
<input type="file" name="fileupload" style="FILTER: alpha(opacity=0); moz-opacity: 0; opacity: 0;" />

// click #selectFile to trigger the click event of input:file

jQuery('#selectFile').live('click',function(){
  var ie = !-[1,]; 
  if(ie){
   jQuery('input:file').trigger('click').trigger('change');
  }else{
   jQuery('input:file').trigger('click');
  }

 });

Select the event that the file will fire

 jQuery('input:file').change(function(){
  //dosomthing
 }); 

Notes:

1. In the chrome browser, the hidden input: file cannot trigger the "click" event for data security.   So set the transparency of the input: file to hide it.


Related articles: