The code copy is as follows:
var ImgObj=new Image(); //Create an image object
var AllImgExt=".jpg|.jpeg|.gif|.bmp|.png|"//All picture format types
var FileObj,ImgFileSize,ImgWidth,ImgHeight,FileExt,ErrMsg,FileMsg,HasCheked,IsImg//Global variable image related attributes
//The following are the limit variables
var AllowExt=".jpg|.gif|.doc|.txt|" //The file type allowed to be uploaded? For unlimited, add a "|" to the lowercase letter after each extension.
var AllowImgFileSize=70; //The size of the allowed uploaded image file is 0 unlimited: KB
var AllowImgWidth=500; //The width of the uploaded image is unlimited: px (pixels)
var AllowImgHeight=500; //The height of the allowed uploaded image? It is unlimited unit: px (pixels)
HasChecked=false;
function CheckProperty(obj) //Detection of image properties
{
FileObj=obj;
if(ErrMsg!="") //Check whether it is the correct image file and return the error message and reset
{
ShowMsg(ErrMsg,false);
return false; //Return
}
ImgFileSize=Math.round(ImgObj.fileSize/1024*100)/100;//Get the size of the image file
ImgWidth=ImgObj.width; //Get the width of the picture
ImgHeight=ImgObj.height; //Get the height of the picture
FileMsg="/n Image size:"+ImgWidth+"*"+ImgHeight+"px";
FileMsg=FileMsg+"/n Image file size:"+ImgFileSize+"Kb";
FileMsg=FileMsg+"/n Image file extension:"+FileExt;
if(AllowImgWidth!=0&&AllowImgWidth<ImgWidth)
ErrMsg=ErrMsg+"/nThe image width exceeds the limit. Please upload a file with a width smaller than "+AllowImgWidth+"px, the current image width is "+ImgWidth+"px;
if(AllowImgHeight!=0&&AllowImgHeight<ImgHeight)
ErrMsg=ErrMsg+"/nThe image height exceeds the limit. Please upload a file with a height less than "+AllowImgHeight+"px, the current image height is "+ImgHeight+"px";
if(AllowImgFileSize!=0&&AllowImgFileSize<ImgFileSize)
ErrMsg=ErrMsg+"/nThe image file size exceeds the limit. Please upload a file smaller than "+AllowImgFileSize+"KB, the current file size is "+ImgFileSize+"KB;
if(ErrMsg!="") ShowMsg(ErrMsg,false);
else ShowMsg(FileMsg,true);
}
ImgObj.onerror=function(){ErrMsg='/nThe image format is incorrect or the image is corrupted!'}
function ShowMsg(msg,tf) //Show prompt message tf=true Show file information tf=false Show error message msg-information content
{
msg=msg.replace("/n","<li>");
msg=msg.replace(//n/gi,"<li>");
if(!tf)
{
FileObj.outerHTML=FileObj.outerHTML;
MsgList.innerHTML=msg;
HasChecked=false;
}else{
if(IsImg) PreviewImg.innerHTML="<img src='"+ImgObj.src+"' width='60' height='60'>";
else PreviewImg.innerHTML="non-image file";
MsgList.innerHTML=msg;
HasChecked=true;
}
}
function CheckExt(obj)
{
ErrMsg="";
FileMsg="";
FileObj=obj;
IsImg=false;
HasChecked=false;
PreviewImg.innerHTML="Preview Area";
if(obj.value=="")return false;
MsgList.innerHTML="File information processing...";
FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();
if(AllowExt!=0&&AllowExt.indexOf(FileExt+"|")==-1) //Judge whether uploading is allowed for file type
{
ErrMsg="/nThis file type is not allowed to be uploaded. Please upload a file of type "+AllowExt+", the current file type is "+FileExt;
ShowMsg(ErrMsg,false);
return false;
}
if(AllImgExt.indexOf(FileExt+"|")!=-1) //If the image file, the image information processing will be performed
{
IsImg=true;
ImgObj.src=obj.value;
alert(ImgObj.src);
alert(Math.round(ImgObj.fileSize/1024*100)/100);
CheckProperty(obj);
return false;
}else{
FileMsg="/nFile extension:"+FileExt;
ShowMsg(FileMsg,true);
}
}