Display selected file in html input before upload

2019-09-12 01:22发布

How can i show file that i select thru browse field into html input? I want to see file that i selected before i press upload?

enter image description here

Here is my code:

          <input id="uploadFile" name="uploadFileOne" type="text" disabled="disabled" value="<?php echo $file_name; ?>" placeholder="EWIS" class="name-info-form file-witdth" />              
          <input class="upload" type="file" id="uploadOne" name="uploadOne" value="Select File"/ >

1条回答
放荡不羁爱自由
2楼-- · 2019-09-12 01:41
<form>
  <input class="upload" type="file" id="uploadOne" name="uploadOne" onchange="setfilename(this.value);" value="Select File"/ >
  <input id="uploadFile" name="uploadFileOne" type="text" disabled="disabled" placeholder="EWIS" class="name-info-form file-witdth" />
</form>

remove value=<?php echo $file_name; ?> and use the javascript for set the name of file in textbox

<script>
  function setfilename(val)
  {
    var fileName = val.substr(val.lastIndexOf("\\")+1, val.length);
    document.getElementById("uploadFile").value = fileName;
  }
</script>

when you select the file using browse button, onchange event will fire and set the file name in text-box :)

查看更多
登录 后发表回答