Home > Web Front-end > JS Tutorial > JavaScript reads the size of image files_javascript skills

JavaScript reads the size of image files_javascript skills

WBOY
Release: 2016-05-16 18:50:55
Original
1254 people have browsed it

As follows:

Copy code The code is as follows:

function checkAttachment(){
alert( "here");
var attachmentArray = document.getElementsByTagName("input");
var attachmentSizeArray = document.getElementsByName("fileMaxSize");
var index = 0;
for (var i = 0; i < attachmentArray.length; i ){
var attachment = attachmentArray[i];
if (attachment.type=="file"){
if (!isPhoto(attachment.value )){
alert("The uploaded attachment must be a photo.");
attachment.focus();
return false;
}
if (getFileSize(attachment) > 1024 * attachmentSizeArray[index].value){
alert("The uploaded attachment cannot be larger than " attachmentSizeArray[index].value "k.");
attachment.focus();
return false;
}
index ;
}
}
return true;
}

function getFileSize(fileObject){
var image=new Image();
image.dynsrc=fileObject.value;
return image.fileSize;
}

The first method is to get all the file controls of the page and the corresponding file size limit,
The key point is the second method, which is very cleverly solved with an image, (*^__^*) Hee hee...
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