Ext.js验证图片大小和尺寸

//验证图片大小
validateImageSize : function (){
	var that = this;
	//取控件DOM对象
    var field = document.getElementById('form-file');//放大图Id
    //取控件中的input元素
    var inputs = field.getElementsByTagName('input');
    var fileInput = null;
    var il = inputs.length;
    //取出input 类型为file的元素
    for(var i = 0; i < il; i ++){
	if(inputs[i].type == 'file'){
	    fileInput = inputs[i];
	    break;
	}
    }
    if(fileInput != null){
	var fileSize = that.getFileSize(fileInput);
	//允许上传200KB~10MB的文件
	if(fileSize<200||fileSize>10240){
		return false;
	}
    }
    return true;
},

//计算文件大小,返回文件大小值,单位K
getFileSize : function (target) {
	var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
    var fs = 0;
    if (isIE && !target.files) {
	var filePath = target.value;
	var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
	var file = fileSystem.GetFile(filePath);
	fs = file.Size;
    } else if (target.files && target.files.length > 0) {
	fs = target.files[0].size;
    } else {
	fs = 0;
    }
    if (fs > 0) {
	fs = fs / 1024;
    }
    return fs;
}


//图片尺寸验证*********onload方法加载慢************
function verificationPicFile(file) {
    var filePath = file.value;
    if(filePath){
        //读取图片数据
        var filePic = file.files[0];
        var reader = new FileReader();
        reader.onload = function (e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var image = new Image();
            image.onload=function(){
                var width = image.width;
                var height = image.height;
                if (width == 720 | height == 1280){
                    alert("文件尺寸符合!");
                }else {
                    alert("文件尺寸应为:720*1280!");
                    file.value = "";
                    return false;
                }
            };
            image.src= data;
        };
        reader.readAsDataURL(filePic);
    }else{
        return false;
    }
}

你可能感兴趣的:(前端页面)