js判断上传文件大小
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; alert(fileSize); }else{ fileSize = target.files[0].size; } var ext=target.value.substring(target.value.lastIndexOf("."),target.value.length).toUpperCase(); if(ext!=".BMP"&&ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"&&ext!=".JPEG"){ alert("图片限于bmp,png,gif,jpeg,jpg格式"); }else{ var size = fileSize / 1024; if(size>30||size==0){ alert("附件大小不能大于30k或为0k!"); var file=document.getElementById(imgId); file.outerHTML=file.outerHTML; $("#"+imgId).attr("src","img/bac_add.jpg"); }else{ var oldImgSrc = $("#"+imgId+"Bac").val(); $("#imgForm").ajaxForm({ url:"newspaper/saveShowImg.html?oldImgSrc="+oldImgSrc+"&imgId="+imgId, type:"post", success:function(data){ $("#"+imgId).attr("src",data); $("#"+imgId+"Bac").val(data); },error:function(data){ } }); $("#imgForm").submit(); } }
<input type="file" id="focusPic" name="focusPic" class="fileupload-hidden" onchange="showCurrentImg(this,'focusImg')"> <img src="img/bac_add.jpg" id="focusImg" class="newsImgList">
function fileChange(target,id) { var fileSize = 0; if (isIE && !target.files) { var filePath = target.value; var fileSystem = new ActiveXObject("Scripting.FileSystemObject"); if(!fileSystem.FileExists(filePath)){ alert("附件不存在,请重新输入!"); var file=document.getElementById(id); file.outerHTML=file.outerHTML; return; } var file = fileSystem.GetFile (filePath); fileSize = file.Size; } else { fileSize = target.files[0].size; } var size = fileSize / 1024; if(size>100000){ alert("附件大小不能大于100M!"); var file=document.getElementById(id); file.outerHTML=file.outerHTML } if(size<=0){ alert("附件大小不能为0M!"); var file=document.getElementById(id); file.outerHTML=file.outerHTML } }