Home >Web Front-end >JS Tutorial >Example of JS detecting image size_javascript skills
< script type="text/javascript">
var maxSize=document.getElementByIdx_x('MAX_FILE_SIZE').value;
function checkFileType(obj){
var path=obj. value.toString();
var perfix=path.substring(path.indexOf('.') 1);
var regExp=/(jpg|jpeg|gif|png)/ gi;
if(perfix.match(regExp)){
checkFileSize(obj); //If the image file type matches successfully, perform file size detection.
}else{
alert('Sorry, please select an image file'); return false;
} }
function checkFileSize(obj){
var img=new Image();
try{
var imgSize=obj.files.item(0).fileSize;
return isLarged(imgSize, maxSize); //Picture file size comparison.
}catch(e){ try{
img.src=obj.value;
return isLarged(img.fileSize,maxSize);
}catch(e){
obj.select();
var path=document.selection.createRange.text();
img.src=path;
return isLarged(img.fileSize,maxSize);
} } }
function isLarged(imgSize,maxSize){
if(imgSize>maxSize){
alert('Sorry, the image file you selected exceeds' parseInt(maxSize/1024) 'Kb');
return false;
}else{
alert(parseInt(imgSize/1024) 'kb ' 'This large is OK');
return true;
} }