如何在文本框中编写要上传的文件的路径? [英] How to write the Path of a file to upload in a text box?

查看:143
本文介绍了如何在文本框中编写要上传的文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小问题:我试图创建一个表单来上传文件。

现在我有一个按钮来选择文件和一个提交按钮。如果我已经选择了一个文件,它的路径(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆