Javascript file suffix

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.