Home > Web Front-end > JS Tutorial > js determines the type and size of uploaded files sample code_javascript skills

js determines the type and size of uploaded files sample code_javascript skills

WBOY
Release: 2016-05-16 17:19:41
Original
1008 people have browsed it
Copy code The code is as follows:

//Detect file size and type
function fileChange(target) {
//Detect the type of uploaded file
if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value))) {
alert("only Allow uploading images in jpg|gif|png|jpeg format");
if(window.ActiveXObject) {//for IE
target.select();//select the file, and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else target. value="";//for FF,Chrome,Safari
return;
} else {
return; //alert("ok");//or you can do nothing here.
}

//Detect the size of the uploaded file
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
if ( isIE && !target.files){
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
} else {
fileSize = target.files[0].size;
}
var size = fileSize / 1024;
if(size>(500) ){
alert("File size cannot exceed 500KB");
if(window.ActiveXObject) {//for IE
target.select();//select the file, and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else {
target.value="";//for FF, Chrome, Safari
}
return;
}else{
return;
}
}
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