input type=file 選擇文件路徑獲取方法


input file上傳按鈕選擇文件后的value是無法直接獲取到的,本文借用js方法獲取file選中文件的物理路徑和文件名

代碼如下:

DOM結構:

<input type="text" id="textfield" /><br/>
<a class="a-upload">
  <input type="file" name="file" id="fileField" onchange="document.getElementById('textfield').value=document.getElementById('fileField').value">點擊這里上傳文件
</a>

<a class="file">選擇文件
  <input type="file" name="file" id="fileField1" onchange="document.getElementById('textfield').value=document.getElementById('fileField1').value">
</a>

CSS樣式:

.a-upload {
    padding: 4px 10px;
    height: 20px;
    line-height: 20px;
    position: relative;
    cursor: pointer;
    color: #888;
    background: #fafafa;
    border: 1px solid #ddd;
    border-radius: 4px;
    overflow: hidden;
    display: inline-block;
    *display: inline;
    *zoom: 1
}

.a-upload  input {
    position: absolute;
    font-size: 100px;
    right: 0;
    top: 0;
    opacity: 0;
    filter: alpha(opacity=0);
    cursor: pointer
}

.a-upload:hover {
    color: #444;
    background: #eee;
    border-color: #ccc;
    text-decoration: none
}

.file {
    position: relative;
    display: inline-block;
    background: #D0EEFF;
    border: 1px solid #99D3F5;
    border-radius: 4px;
    padding: 4px 12px;
    overflow: hidden;
    color: #1E88C7;
    text-decoration: none;
    text-indent: 0;
    line-height: 20px;
}
.file input {
    position: absolute;
    font-size: 100px;
    right: 0;
    top: 0;
    opacity: 0;
}
.file:hover {
    background: #AADFFD;
    border-color: #78C3F3;
    color: #004974;
    text-decoration: none;
}

獲取file選擇文件名方法

this.files[0].name
獲取file選擇文件物理路徑方法
document.getElementById('fileField').value

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM