代码如下 复制代码 代码如下: 无标题文档 <BR>//本地图片预览 <BR>function setImagePreview(fieldupload, image, imagediv) { <BR>var docObj = document.getElementById(fieldupload); <BR>var imgObjPreview = document.getElementById(image); <BR>if (docObj.files && docObj.files[0]) { <BR>//火狐下,直接设img属性 <BR>imgObjPreview.style.display = 'block'; <BR>imgObjPreview.style.width = '150px'; <BR>imgObjPreview.style.height = '150px'; <BR>//imgObjPreview.src = docObj.files[0].getAsDataURL(); <BR>//火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式 <BR>imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]); <BR>} else { <BR>//IE下,使用滤镜 <BR>docObj.select(); <BR>var imgSrc = document.selection.createRange().text; <BR>var localImagId = document.getElementById(imagediv); <BR>//必须设置初始大小 <BR>localImagId.style.width = "150px"; <BR>localImagId.style.height = "150px"; <BR>//图片异常的捕捉,防止用户修改后缀来伪造图片 <BR>try { <BR>localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc; <BR>} catch (e) { <BR>alert("您上传的图片格式不正确,请重新选择!"); <BR>return false; <BR>} <BR>imgObjPreview.style.display = 'none'; <BR>document.selection.empty(); <BR>} <BR>return true; <BR>} <BR>