Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 10 people (70%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

how can we get just file name when we use input type is file. how can we skip the path of the field.

Apr 7th, 2008 22:55
ha mo, Abolfazl Shirazi, saba shafi,


Abolfazl Shirazi:
Your question shows that you don't know anything about two important 
string methods like string.substr() and substring(). 
Well, With using these methods, Your question is very easy right now 
and you can specify the filename, the file extension and the file 
destination from the string that you give from <input> element! As you 
know, a filename cannot contain any \ (backslash) or . (dot) 
character. We use this feature to solve our problem.
Try the page below:
<!-- Begin -->
<html>
<head>
<script language="javascript">
function SN() {
MyString = MyField.value
for (i=MyString.length-1;i>=0;i--) {
	if (MyString.charAt(i) == '\\') {
		m = i ;
		break ;
	}
}
for (i=MyString.length-1;i>=0;i--) {
	if (MyString.charAt(i) == '.') {
		n = i ;
		break ;
	}
}
alert(MyString.substring(m+1,n))
}
function SE() {
MyString = MyField.value
for (i=MyString.length-1;i>=0;i--) {
	if (MyString.charAt(i) == '.') {
		m = i ;
		break ;
	}
}
alert(MyString.substring(m+1))
}
function SD() {
MyString = MyField.value
for (i=MyString.length-1;i>=0;i--) {
	if (MyString.charAt(i) == '\\') {
		m = i ;
		break ;
	}
}
alert(MyString.substring(0,m))
}
</script>
</head>
<body>
<input type="file" name="MyField">
<p>
<button onclick="SN()">Show File Name</button><br>
<button onclick="SE()">Show File Extension</button><br>
<button onclick="SD()">Show File Destination</button><br>
If you see a script error while using this page, It maybe because you 
have not entered a valid file location in the input field.
</body>
</html>
<! -- End -->
Works Successfully in IE6.
Enjoy!
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com