javascript - manipulate html input type file filename in firefox - Stack Overflow

I have a html input type file control. When I select a file with very big name, it shows plete filename

I have a html input type file control. When I select a file with very big name, it shows plete filename in firefox which causes bad UI. Is there any solution for this problem like changing name etc?

I have a html input type file control. When I select a file with very big name, it shows plete filename in firefox which causes bad UI. Is there any solution for this problem like changing name etc?

Share Improve this question asked Jul 15, 2013 at 5:32 PopeyePopeye 1,5583 gold badges30 silver badges42 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

You can handle it this way:

  1. Make your html input file control hidden and add onchange event handler to change selected file name
  2. Add a readonly html textbox control for showing changed file name
  3. Add html button control with onclick event handler to trigger file control's click event

DEMO here

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;
}

There is three possible answers I know:

  1. You can style size of your input, and then browser cut longer names to given size (with '...' at the end of visible part).
  2. You can not change from script the value of file input for security reason. The same is with trigger its click event. So the answer in this case is No.
  3. You can cover whole file input with "pseude custom css". It is not real input styling but uses some trick with opacity. Look here for more details.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745481474a4629576.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信