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

Example of JS detecting image size_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 17:24:54
Original
945 people have browsed it

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;

} }

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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template