Js gets and clears the input type='file' value sample code

  • 2020-03-30 01:48:33
  • OfStack

Basic knowledge of upload control:

Upload control (< Input type = "file" / >) For browsing and uploading files on the client side, the path selected by the user can be obtained by the value attribute, but the value attribute is read-only and cannot be assigned by javascript, which makes it impossible to clear it by the value="" statement. It's easy to see why read-only, if you can assign random values, users can simply open your web page and you can upload files on their computer at will.

Js access < Intput type = file / > The value of the


<html>
 <script language='javascript'>   
  function   show(){   
  var   p=document.getElementById("file1").value;  
  document.getElementById("s").innerHTML="<input id=pic type=image height=96 width=128 /> ";   
  document.getElementById("pic").src=p;
  alert(p);    
  }   
  </script>
 <head>
  <title>MyHtml.html</title>
 </head>
 <body>
  <input type="file" name="file1" id="file1" onpropertychange="show();" />
  <span id="s"></span>
 </body>
</html>

Clear the upload control (< Input type = "file" / >) Two methods of the value of

Method 1:

<span   id=span1>   
  <input   name=ab   type=file>   
  </span>   
  <input   name=button1   type=button   value=" According to the "   onclick=show()>  
  <script   language=javascript>   
  function   show()   
  {   
  document.getElementById("span1").innerHTML="<input   name=ab   type=file>";   
  }   
  </script>  

Method 2:

function clearFileInput(file){
    var form=document.createElement('form');
    document.body.appendChild(form);
    //Remember the location of the file in the old form
    var pos=file.nextSibling;
    form.appendChild(file);
    form.reset();
    pos.parentNode.insertBefore(file,pos);
    document.body.removeChild(form);
}


Related articles: