4

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

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Florian Müller
  • 7,448
  • 25
  • 78
  • 120

3 Answers3

7

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/)

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

If you want to upload a file, use <input type="file" …> and it will come with it's own button. Don't forget to set the enctype.

A regular text box won't let you upload files.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • yes, i know, but i thought, is a button, and if i select with this one a file, the path of it will be shown in a textfield. This is much more user-friendly – Florian Müller Jan 09 '11 at 16:09
  • By default, most browsers display `` as a widget consisting of a text box and a button … which is what it sounds like you want. So what is the problem? – Quentin Jan 09 '11 at 16:21
  • Chrome show the selected file as "label", looks like the OP want it inside a textbox in addition. :) – Shadow The GPT Wizard Jan 09 '11 at 16:26
0

The <input type="file"..> will not show textbox in chrome and safri browser, we can configure the display styles by CSS itself, go to the link here

kark
  • 4,763
  • 6
  • 30
  • 44