Forum Moderators: open

Message Too Old, No Replies

Passing File Name Only from a File Field to input box

         

walker

12:18 pm on Apr 2, 2003 (gmt 0)

10+ Year Member



Hi

Does anyone know how to send just the file name value itself to a new text input box:

when someone selects a file via the browse button the field will display something like:

c:\thedirectory\sub\Test.Gif

I would like to pass this information via an onclick command to another Form Field (Normal input NOT file) but just the name itself:

Test.Gif

Any Ideas?

Kind Regards

Walker

ShawnR

12:40 pm on Apr 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a few ways you can extract a substring from a string. One way is:

If MyString contained "c:\thedirectory\sub\Test.Gif", then

MyString.lastIndexOf('\');
would return 19, the position of the last slash.
MyString.length
would return 28. You can then use
MyString.substring[m, n]
to extract Test.Gif.

An alternative method is to use MyString.split('\'), which would return an array containing "c:", "thedirectory", etc

Beware that you should check for '/' as well, and also handle the case where the '/' or '\' is not found.

Shawn

walker

1:21 pm on Apr 2, 2003 (gmt 0)

10+ Year Member



Looks good but how do I apply that to code, sorry if I am being a little Blonde

Walker

ShawnR

2:17 pm on Apr 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi

I'm not sure what your level of knowledge is, so this is difficult to answer... If you know nothing about javascript, then you should search the web and also search this site (see the link at the top of the page) for "javascript tutorial". Below are a couple of tips just to get you started...

In your html file, you can define functions like so:

<script language="javascript">
function sayHello() {
alert('hello world');
}
</script>

or you can put

function sayHello() {
alert('hello world');
}
in a file called filename.js, and then put the following in the html file:
<script type="text/javascript" language="javascript" src="filename.js"></script>

Then your onclick tag attribute can be set to

onclick="sayHello()"

To get to the form fields, you need to name the form and name the fields. Then in your function, you can do:

MyString = document.forms['FormName'].elements['x'].value;

If you know javascript and I am misunderstanding your question, please clarify.

Shawn

walker

5:07 pm on Apr 2, 2003 (gmt 0)

10+ Year Member



Hi

I know enough about JS to get by. What I meant was how do I impliment the lines of code provided to do the job I mentioned at the begining.

I have worked this out now myself the simplified version of the code is as follows:

<form name="TestForm">
<input type="file" name="TheFile">
<input type="text" name="NameOnly">
</form>

<SCRIPT>

function ExtractName(){

var Data = document.TestForm.TheFile.value;

var a = Data.lastIndexOf('.');

var b = Data.Length
var FileNameOnly = Data.substring(a+1, b)

TestForm.NameOnly.value=FileNameOnly

}
</SCRIPT>
<a onclick="ExtractName()">SUBMIT</a>

The Problem is that lastindexOf works with any char other than "\" (is currently set to .) why is this and is there a way to make it work?

Kind Regards

Walker

walker

5:58 pm on Apr 2, 2003 (gmt 0)

10+ Year Member



Hi

Cracked it :) -- Here is the Solution.

var a = Data.lastIndexOf('\\');

Kind Regards

Walker