The following section describes how to use js to verify the upload of images and how to control the extension of an uploaded file. If you are interested, refer to the following:
Js verification upload Image
The Code is as follows:
Var ImgObj = new Image (); // create an Image object
VarAllImgExt = ". jpg |. jpeg |. gif |. bmp |. png |" // All image formats
VarFileObj, ImgFileSize, ImgWidth, ImgHeight, FileExt, ErrMsg, FileMsg, IsImg // global variable image attributes
// The following are restricted Variables
Var AllowExt = ""; // the file type that can be uploaded is unlimited. A "|" lowercase letter indicates that each extension must be followed.
Var AllowImgFileSize = 100; // the size of the image file that can be uploaded is 0. The unit is KB.
Var AllowImgWidth = 385; // The Image Width limit that can be uploaded is unlimited. Unit: px (pixel)
Var AllowImgHeight = 441; // The height of the image that can be uploaded is unlimited. Unit: px (pixel)
Function CheckProperty (obj) // checks image attributes.
{
FileObj = obj;
If (ImgObj. readyState! = "Complete") // performs cyclic detection if the image is not loaded
{
SetTimeout ("CheckProperty (FileObj)", 500 );
Return false;
}
ImgFileSize = Math. round (ImgObj. fileSize/1024*100)/100; // obtain the image file size
ImgWidth = ImgObj. width; // obtain the image width.
ImgHeight = ImgObj. height; // obtain the Image height.
FileMsg = "\ n image size:" + ImgWidth + "*" + ImgHeight + "px ";
FileMsg = FileMsg + "\ n image file size:" + ImgFileSize + "Kb ";
FileMsg = FileMsg + "\ n Image File Extension:" + FileExt + "\ n can be uploaded! ";
ErrMsg = "";
If (AllowImgWidth! = ImgWidth)
ErrMsg = ErrMsg + "\ n please upload a file whose width is equal to" + AllowImgWidth + "px. The current image width is" + ImgWidth + "px ";
If (AllowImgHeight! = ImgHeight)
ErrMsg = ErrMsg + "\ n upload a file with a height equal to" + AllowImgHeight + "px. The current image height is" + ImgHeight + "px ";
If (AllowImgFileSize! = 0 & AllowImgFileSizeErrMsg = ErrMsg + "\ n 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 = '\ n incorrect image format or damaged image! ';}
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 is used, the image information is processed.
{
IsImg = true;
FileObj = obj;
ImgObj. src = obj. value;
ReturnCheckProperty (obj );
} Else
{
Alert ("this file type cannot be uploaded. Upload the "+ AllImgExt +" type file, \ n the current file type is "+ FileExt );
Obj. value = '';
Return false;
}
}
Upload js Verification
The following describes how to control the extension of an uploaded file.
Js:
The Code is as follows:
Function check2 ()
{
Var file = document. getElementsByName ("file"). value;
If (file = "")
{
Alert ("select a file ");
Return false;
}
Var strTemp = file. split (".");
Var strCheck = strTemp [strTemp. length-1];
If (strCheck. toUpperCase () = 'jpg ')
{
Return true;
} Else
{
Alert ('upload file type is incorrect! ');
Return false;
}
}
Form:
The Code is as follows:
Note that document. getElementsByName ("file "). the value obtains the absolute path of the uploaded file. Therefore, the extended name of the file is separated by the string splitting method and then determined.