![](/img/trans.png)
[英]How to get the filename from input type=file html element using JavaScript or JQuery?
[英]manipulate html input type file filename in firefox
我有一個html輸入類型文件控件。 當我選擇一個名字很大的文件時,它會在firefox中顯示完整的文件名,這會導致錯誤的UI。 有沒有解決這個問題的方法,如更改名稱等?
你可以這樣處理它:
input file
控件隱藏,並添加onchange
事件處理程序以更改選定的文件名 textbox
控件,以顯示更改的文件名 onclick
事件處理程序添加html button
控件以觸發文件控件的單擊事件 HTML:
<input type="text" id="txtFile" readonly="true" />
<input type="button" id="btn" value="Browse..." onclick="browseFile();" />
<input type="file" id="file1" name="file1" onchange="setFileName(this.value);" />
CSS:
#file1 {
display: none;
}
JS:
function browseFile() {
document.getElementById('file1').click();
}
function setFileName(fileName) {
/* Manipulate file name here */
fileName = fileName.substr(0, fileName.lastIndexOf('.'));
document.getElementById('txtFile').value = fileName;
}
我知道有三種可能的答案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.