如何在文本框中编写要上传的文件的路径? [英] How to write the Path of a file to upload in a text box?
问题描述
小问题:我试图创建一个表单来上传文件。
现在我有一个按钮来选择文件和一个提交按钮。如果我已经选择了一个文件,它的路径(C:\ Users ...)显示在一个文本框中,我怎样才能设计它?`
/ p>
Thx for help
复制所选文件名称/路径到不同的文本框,首先有这个JS:
$ p $函数CopyMe(oFileInput,sTargetID){
document.getElementById sTargetID).value = oFileInput.value;
}
它可以使用这样的HTML:
< div>
< input type =fileonchange =CopyMe(this,'txtFileName'); />
< / div>
< div>
您选择了:< input id =txtFileNametype =textreadonly =readonly/>
< / div>
测试用例: http://jsfiddle.net/yahavbr/gP7Bz/
请注意现代浏览器会隐藏显示类似于 C:\ fakepath\realname.txt
所以只显示名称(这是真实的)改为:
函数CopyMe(oFileInput,sTargetID){
var arrTemp = oFileInput.value.split('\\\');
document.getElementById(sTargetID).value = arrTemp [arrTemp.length - 1];
( http://jsfiddle.net/yahavbr/gP7Bz/1/ )
Little question: I'm trying to create a Form to upload a file.
Now i got a button to select the file and a submit button.
How can i design it like if i've selected a file, the path of it (C:\Users....) is shown in a textbox?`
Thx for help
To copy the selected file name/path to different text box, first have this JS:
function CopyMe(oFileInput, sTargetID) {
document.getElementById(sTargetID).value = oFileInput.value;
}
And it will work with such HTML:
<div>
<input type="file" onchange="CopyMe(this, 'txtFileName');" />
</div>
<div>
You chose: <input id="txtFileName" type="text" readonly="readonly" />
</div>
Test case: http://jsfiddle.net/yahavbr/gP7Bz/
Note that modern browsers will hide the real full path showing something like C:\fakepath\realname.txt
so to show only the name (which is real) change to:
function CopyMe(oFileInput, sTargetID) {
var arrTemp = oFileInput.value.split('\\');
document.getElementById(sTargetID).value = arrTemp[arrTemp.length - 1];
}
(http://jsfiddle.net/yahavbr/gP7Bz/1/)
这篇关于如何在文本框中编写要上传的文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!