This article mainly introduces how to verify the upload of images by js. it is very simple and practical to restrict the type and size of the uploaded images, for more information about how to upload images, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:
Js verification imageScript UpLoadFileCheck = function () {this. allowExt = ".jpg,.gif"; // the file type 0 that is allowed to be uploaded is unlimited. // A "," lowercase letter indicates this. allowImgFileSize = 0; // The size of the file to be uploaded 0 is unrestricted unit: KB this. allowImgWidth = 0; // the image width 0 that can be uploaded is unrestricted unit: px (pixel) this. allowImgHeight = 0; // The height of the image to be uploaded 0 is unrestricted unit: px (pixel) this. imgObj = new Image (); this. imgFileSize = 0; this. imgWidth = 0; this. imgHeight = 0; this. fileExt = ""; this. errMsg = ""; this. isImg = false; // global variable} UpL OadFileCheck. prototype. checkExt = function (obj) {this. errMsg = ""; this. imgObj. src = obj. value; // this. hasChecked = false; if (obj. value = "") {this. errMsg = "\ n select a file";} else {this. fileExt = obj. value. substr (obj. value. lastIndexOf (". ")). toLowerCase (); if (this. allowExt! = 0 & this. allowExt. indexOf (this. fileExt) =-1) // Determine whether the file type can be uploaded {this. errMsg = "\ n this file type cannot be uploaded. Upload a file of the "+ this. AllowExt +" type. the current file type is "+ this. FileExt ;}} if (this. ErrMsg! = "") {This. showMsg (this. errMsg, false); return false;} else return this. checkProperty (obj);} UpLoadFileCheck. prototype. checkProperty = function (obj) {if (this. imgObj. readyState! = "Complete") // {sleep (1000); // The image can be fully loaded in one second.} if (this. isImg = true) {this. imgWidth = this. imgObj. width; // Obtain the image width. this. imgHeight = this. imgObj. height; // Obtain the image height if (this. allowImgWidth! = 0 & this. AllowImgWidth
"); Msg = msg. replace (/\ n/gi ,"
"); */Alert (msg);} function sleep (num) {var tempDate = new Date (); var tempStr =" "; var theXmlHttp = new ActiveXObject (" Microsoft. XMLHTTP "); while (new Date ()-tempDate)
I hope this article will help you design javascript programs.