Copy codeThe Code is as follows:
// Check the file size and type
Function fileChange (target ){
// Check the type of the uploaded file
If (! (/(? : Jpg | gif | png | jpeg) $/I. test (target. value ))){
Alert ("only jpg files can be uploaded | gif | png | jpeg images ");
If (window. ActiveXObject) {// for IE
Target. select (); // select the file, and clear selection
Document. selection. clear ();
} Else if (window. opera) {// for opera
Target. type = "text"; target. type = "file ";
} Else target. value = ""; // for FF, Chrome, Safari
Return;
} Else {
Return; // alert ("OK"); // or you can do nothing here.
}
// Check the size of the uploaded file
Var isIE =/msie/I. test (navigator. userAgent )&&! Window. opera;
Var fileSize = 0;
If (isIE &&! Target. files ){
Var filePath = target. value;
Var fileSystem = new ActiveXObject ("Scripting. FileSystemObject ");
Var file = fileSystem. GetFile (filePath );
FileSize = file. Size;
} Else {
FileSize = target. files [0]. size;
}
Var size = fileSize/1024;
If (size & gt; (500 )){
Alert ("the file size cannot exceed 500KB ");
If (window. ActiveXObject) {// for IE
Target. select (); // select the file, and clear selection
Document. selection. clear ();
} Else if (window. opera) {// for opera
Target. type = "text"; target. type = "file ";
} Else {
Target. value = ""; // for FF, Chrome, Safari
}
Return;
} Else {
Return;
}
}