When uploading a file, you often need to determine the file type, that is, the extension name of the file. This can be easily done using JavaScript. There are many methods to parse an absolute path file name and get the suffix by using JavaScript. One is listed here for your reference.
For a file name with an absolute path, such as: D: \ Program Files \ notepad ++ \ notepad1_cmd.exe
First, to avoid escape backslash problems, you can use a regular expression to replace \ or \ with #, such:
D: # program files?notepad={{notepad={.exe
Then, use '#' as the separator to break the string into an array and obtain the following array:
D: ProgramFiles notepad ++ notepad1_cmd.exe
Take the last file name of the array with a suffix: notepad1_0000.exe
Separate the filename with a suffix into an array using the separator '.' to obtain the following array:
Notepad ++ exe
Then retrieve the last one of the array to get the file suffix exe.
The Code is as follows (win7 + ie9 passed the test ):
//by MoreWindows (http://blog.csdn.net/MoreWindows)function GetExtensionFileName(pathfilename){var reg = /(\\+)/g;var pfn = pathfilename.replace(reg, "#");var arrpfn = pfn.split("#");var fn = arrpfn[arrpfn.length - 1];var arrfn = fn.split(".");return arrfn[arrfn.length - 1];}
Test code:
function Test(){var filePath="D:\\Program Files\\Notepad++\\Notepad++.exe";alert(GetExtensionFileName(filePath));}<input type="button" value="Test" onclick="Test()" />
Click the "test" button to bring up the "EXE" dialog box, indicating that getextensionfilename can correctly parse the file name with an absolute path and get the suffix.
It is estimated that this method can only be used on the Windows platform. I don't know what it will do in Linux?
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/7055713