Js is used to obtain the uploaded file name for pure beautification.

Source: Internet
Author: User

Why use js to get the uploaded file name? In fact, it is purely for beautification and friendliness,

However, this is not the case on the Internet. Use path. substring (path. lastIndexOf ('/') + 1 );

This may cause many problems at work.
For example, the file name can be obtained by default for the value of firefox input form, while ie displays the file path.

In firefox, lastIndexOf ('/') gets-1, while in ie, the directory displays a backslash, \, of course, in Linux
Other browsers get the forward slash (/). For the moment, it is not proved that the file value obtained by a browser in Linux is the full path );
Two judgments are required:
Copy codeThe Code is as follows:
Function getFileName (path ){
Var pos1 = path. lastIndexOf ('/');
Var pos2 = path. lastIndexOf ('\\');
Var pos = Math. max (pos1, pos2)
If (pos <0)
Return path;
Return path. substring (pos + 1 );

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.