Upload js verification (image/file extension)

Source: Internet
Author: User

Js verification upload Image
Copy codeThe 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 & AllowImgFileSize ErrMsg = ErrMsg + "\ n upload files 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
<Form action = "*. jsp" method = "post" onsubmit = "return check ()">
<Input type = "file" name = "file">
<Input type = "submit" value = "Upload">
</Form>

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.