var ImgObj =new Image();//Create an image object
varAllImgExt=".jpg|.jpeg|.gif|.bmp|.png|"//All image format types
varFileObj,ImgFileSize,ImgWidth,ImgHeight ,FileExt,ErrMsg,FileMsg,IsImg//Global variable image related attributes
//The following are restriction variables
var AllowExt=""; //The file types allowed to be uploaded are unlimited. Each extension must be followed by Add a "|" lowercase letter to represent
var AllowImgFileSize=100; //The size of the image file allowed to be uploaded 0 is unlimited Unit: KB
var AllowImgWidth=385; //The width of the image file allowed to be uploaded Ɓ is none Restriction unit: px (pixel)
var AllowImgHeight=441; //The height ƹ of the uploaded image is allowed to be unlimited. Unit: px (pixel)
function CheckProperty(obj) //Detect image properties
{
FileObj=obj;
if(ImgObj.readyState!="complete")//If the image is not loaded, perform loop detection
{
setTimeout("CheckProperty(FileObj)",500) ;
return false;
}
ImgFileSize=Math.round(ImgObj.fileSize/1024*100)/100;//Get the size of the image file
ImgWidth=ImgObj.width;//Get The width of the image
ImgHeight=ImgObj.height; //Get the height of the image
FileMsg="nImage size:" ImgWidth "*" ImgHeight "px";
FileMsg=FileMsg "nImage file size:" " ImgFileSize "Kb";
FileMsg=FileMsg "nImage file extension:" FileExt "nCan be uploaded! ";
ErrMsg="";
if(AllowImgWidth!=ImgWidth)
ErrMsg =ErrMsg "nPlease upload a file with a width equal to " AllowImgWidth "px. The current image width is " ImgWidth "px";
if(AllowImgHeight!=ImgHeight)
ErrMsg=ErrMsg "nPlease upload a file with a height equal to " AllowImgHeight " px file, the current image height is " ImgHeight "px";
if(AllowImgFileSize!=0&&AllowImgFileSize
ErrMsg=ErrMsg "nPlease upload a file smaller than " AllowImgFileSize "KB, the current file size is " ImgFileSize "KB";
if(ErrMsg!="")
{
alert(ErrMsg);
return false;
}
else
return true;
}//end CheckProperty();
ImgObj.onerror=function(){ErrMsg='nThe image format is incorrect or the image is damaged!';}
function CheckExt(obj)
{
ErrMsg="";
FileMsg="";
IsImg=false;
if(obj.value=="")
return false;
FileExt=obj.value.substr( obj.value.lastIndexOf(".")).toLowerCase();
if(AllImgExt.indexOf(FileExt "|")!=-1)//If the image file, perform image information processing
{
IsImg=true;
FileObj=obj;
ImgObj.src=obj.value;
returnCheckProperty(obj);
}else
{
alert("This file Type not allowed to be uploaded. Please upload a file of type "AllImgExt", the current file type is "FileExt);
obj.value='';
return false;
}
}
It should be noted that document.getElementsByName("file").value obtains the absolute path of the uploaded file, so the string splitting method is used to split the extension of the file, and then the judgment is made.