<div class="codetitle"> <span><a style="CURSOR: pointer" data="3713" class="copybut" id="copybut3713" onclick="doCopy('code3713')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code3713"> <br><div id="imgbox"> <br> <img id="imghead" src="img/no_photo.gif" alt="" style="max-width:90%" style="max-width:90%"><br> </div> <br><fileupload id="FileUploadImg" runat="server" onchange="previewImage(this);"> style="position:relative;left:-130px;cursor:pointer;"/><br>var $$ = function(id) {<br> return "string" == typeof id ? document.getElementById(id) : id;<br>};<br>/*<br>图片验证<br>*/<br>//验证上传图片格式<br>function checkupload(ele) {<br> var obj = $$("FileUploadImg");<br> var fileContentType = obj.value.match(/^(.*)(\.)(.{1,8})$/)[3]; //这个文件类型正则很有用:) <br> if (fileContentType == 'gif' || fileContentType == 'jpeg' || fileContentType == 'png' || fileContentType == 'jpg') {<br> //验证上传图片大小<br> return checkFileSize(ele);<br> } else {<br> alert('上传文件格式不正确!');<br> return false;<br> }<br> return false;<br>}<br>//验证上传图片大小<br>function checkFileSize(ele) {<br> var maxSize = 100 * 1024;<br> //ie<br> if (window.navigator.userAgent.indexOf("MSIE") >= 1) {<br> var fso;<br> try {<br> fso = new ActiveXObject('Scripting.FileSystemObject');<br> var file = fso.GetFile(ele.value);<br> //alert(file.Size);<br> return file.Size } catch (e) {<br> alert("浏览器设置禁用ActiveXObject控件,请启用");<br> return false;<br> }<br> }<br> //firefox<br> else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {<br> //alert(ele.files[0].fileSize);<br> return ele.files[0].fileSize }<br> return false;<br>}<br>//加载预览图片<br>function previewImage(ele) {<br> if (ele && checkupload(ele)) {<br> var imgDiv = $$("imgbox");<br> imgDiv.innerHTML = "";<br> imgDiv.style.width = "100px";<br> imgDiv.style.height = "125px";<br> //ie<br> if (window.navigator.userAgent.indexOf("MSIE") >= 1) {<br> imgDiv.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale)";<br> imgDiv.filters("DXImageTransform.Microsoft.AlphaImageLoader").src = ele.value;<br> }<br> //firefox<br> else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {<br> imgDiv.innerHTML = "<img style="max-width:90%" + 100 + "' height='" + 125 + "' id="aPic" src="%22%20+%20ele.files.item(0).getAsDataURL()%20+%20%22" alt="图片上传判断及预览脚本的效果实例_javascript技巧" >";<br> } else {<br> imgDiv.innerHTML = "<span style="color:red;">IE/火狐浏览器才支持图片预览</span>";<br> }<br> } else {<br> $$("FileUploadImg").value = "";<br> $$("imgbox").innerHTML = "<img id="imghead" src="img/no_photo.gif" alt="" style="max-width:90%" style="max-width:90%">";<br> //alert('重置上传控件');<br> }<br>}<br></fileupload> </div>