Copy codeThe Code is as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Input type = "hidden" name = "MAX_FILE_SIZE" id = "MAX_FILE_SIZE" value = "8000000"/>
<Input type = "file" name = "files" id = "files" onchange = "checkFileType (this)"/>
<Script type = "text/javascript">
Var maxSize = document. getElementByIdx_x ('max _ FILE_SIZE '). value;
Function checkFileType (obj ){
Var path = obj. value. toString ();
Var perfix = path. substring (path. indexOf ('.') + 1 );
Var regExp =/(jpg | jpeg | gif | png)/gi;
If (perfix. match (regExp )){
CheckFileSize (obj); // if the image file type matches successfully, check the file size.
} Else {
Alert ('Sorry, please select image file'); return false;
}}
Function checkFileSize (obj ){
Var img = new Image ();
Try {
Var imgSize = obj. files. item (0). fileSize;
Return isLarged (imgSize, maxSize); // compare the image file size.
} Catch (e) {try {
Img. src = obj. value;
Return isLarged (img. fileSize, maxSize );
} Catch (e ){
Obj. select ();
Var path = document. selection. createRange. text ();
Img. src = path;
Return isLarged (img. fileSize, maxSize );
}}}
Function isLarged (imgSize, maxSize ){
If (imgSize> maxSize ){
Alert ('Sorry, the image file you selected exceeds '+ parseInt (maxSize/1024) + 'kb ');
Return false;
} Else {
Alert (parseInt (imgSize/1024) + 'kb' + 'this large is OK ');
Return true;
}}</Script>