Home  >  Article  >  Web Front-end  >  Example of JS detecting image size_javascript skills

Example of JS detecting image size_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:24:54848browse

Copy code The code is as follows:





< 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;

} }

Statement:
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