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

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

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

Method 1:

<span   id=span1>   
  <input   name=ab   type=file>   
  <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>";   

Method 2:

function clearFileInput(file){
    var form=document.createElement('form');
    //Remember the location of the file in the old form
    var pos=file.nextSibling;

Related articles: