Js obtains sample code for determining the suffix of the uploaded file

Source: Internet
Author: User

Copy codeThe Code is as follows:
Function lastname (){
// Obtain the path of the file to be uploaded
Var filepath = document. getElementById ("file1"). value;
// To avoid escape backslash problems, convert it here
Var re =/(\ +)/g;
Var filename = filepath. replace (re ,"#");
// Cut the path string
Var one = filename. split ("#");
// Obtain the last object name in the array.
Var two = one [one. length-1];
// Truncate the file name to get the suffix
Var three = two. split (".");
// Obtain the last intercepted string, that is, the suffix
Var last = three [three. length-1];
// Add the suffix type to be determined
Var tp = "jpg, gif, bmp, JPG, GIF, BMP ";
// Return the position of the qualified Suffix in the string
Var rs = tp. indexOf (last );
// If the returned result is greater than or equal to 0, it indicates the file type that can be uploaded.
If (rs> = 0 ){
Return true;
} Else {
Alert ("the uploaded file you selected is not a valid image file! ");
Return false;
}
}

Note:
1. Save the script as a JS file and include it on the page of the uploaded file;
2. Add onsubmit = "return lastname ()" to the form on the upload page ()"

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.