现在的位置: 首页 > 综合 > 正文

监测文件大小

2018年05月22日 ⁄ 综合 ⁄ 共 914字 ⁄ 字号 评论关闭
//监测文件大小,不允许上传超过5M的文件
function getFileSize(filePath) {   
	var isIE=!!window.ActiveXObject;
	var isIE6=isIE && !window.XMLHttpRequest;
	var isIE8=isIE && !!document.documentMode;
	var isIE7=isIE && !isIE6 && !isIE8;
	if (isIE){
		if (isIE6){
			var image = new Image();
			image.dynsrc= filePath.value;
			if(image.fileSize > 5242880) {
				alert("上传附件大小不允许超过5M!");
				clearFileInput(filePath);
				return;
			}
		}else if (isIE8){
			var fso = new ActiveXObject("Scripting.FileSystemObject");
			if(fso.GetFile(filePath.value).size > 5242880) {
				alert("上传附件大小不允许超过5M!");
				clearFileInput(filePath);
				return;
			}
		}else if (isIE7){
			var fso = new ActiveXObject("Scripting.FileSystemObject");
			if(fso.GetFile(filePath.value).size > 5242880) {
				alert("上传附件大小不允许超过5M!");
				return;
			}
		} else {
			
		}
	}
}

function clearFileInput(file){
    var form=document.createElement('form');
    document.body.appendChild(form);
    //记住file在旧表单中的的位置
    var pos=file.nextSibling;
    form.appendChild(file);
    form.reset();
    pos.parentNode.insertBefore(file,pos);
    document.body.removeChild(form);
}

抱歉!评论已关闭.