Home > Web Front-end > JS Tutorial > Uploaded js verification (image/file extension)_javascript skills

Uploaded js verification (image/file extension)_javascript skills

WBOY
Release: 2016-05-16 17:35:25
Original
1129 people have browsed it
js verification uploaded image
Copy code The code is as follows:

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&&AllowImgFileSizeErrMsg=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;
}
}

Uploaded js verification
The following will introduce how to control the extension of an uploaded file
js:
Copy code<. 🎜> The code is as follows:
function check2()
{
var file = document.getElementsByName("file").value;
if( file=="")
{
alert("Please select a file");
return false;
}
var strTemp = file.split(".");
var strCheck = strTemp[strTemp.length-1];
if(strCheck.toUpperCase()=='JPG')
{
return true;
}else
{
alert('The uploaded file type is incorrect!');
return false;
}
}

Form:

Copy code The code is as follows:





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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template