Js gets the sample code that determines the suffix name of the uploaded file
- 2020-03-30 01:48:45
- OfStack
function lastname(){
//Gets the file path to upload
var filepath = document.getElementById("file1").value;
//To avoid the problem of escaping the backslash, it will be converted here
var re = /(\+)/g;
var filename=filepath.replace(re,"#");
//Cut and intercept the path string
var one=filename.split("#");
//Gets the last file name in the array
var two=one[one.length-1];
//Then intercept the file name to get the suffix
var three=two.split(".");
//Gets the last string intercepted, which is the suffix name
var last=three[three.length-1];
//Add the suffix name type to be judged
var tp ="jpg,gif,bmp,JPG,GIF,BMP";
//Returns the position of the qualified suffix in the string
var rs=tp.indexOf(last);
//If the result returned is greater than or equal to 0, the type of file that is allowed to be uploaded is included
if(rs>=0){
return true;
}else{
alert(" The upload file you selected is not a valid image file! ");
return false;
}
}
Remarks:
1. Save the script as a JS file and include it in the page where the file is uploaded;
2. Add onsubmit="return lastname()" to the form of the uploaded page