复制代码 代码如下: Image preview example <BR>var loadImageFile = (function () { <BR>if (window.FileReader) { <BR>var oPreviewImg = null, oFReader = new window.FileReader(), <BR>rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i; <br><br>oFReader.onload = function (oFREvent) { <BR>if (!oPreviewImg) { <BR>var newPreview = document.getElementById("imagePreview"); <BR>oPreviewImg = new Image(); <BR>oPreviewImg.style.width = (newPreview.offsetWidth).toString() + "px"; <BR>oPreviewImg.style.height = (newPreview.offsetHeight).toString() + "px"; <BR>newPreview.appendChild(oPreviewImg); <BR>} <BR>oPreviewImg.src = oFREvent.target.result; <BR>}; <br><br>return function () { <BR>var aFiles = document.getElementById("imageInput").files; <BR>if (aFiles.length === 0) { return; } <BR>if (!rFilter.test(aFiles[0].type)) { alert("You must select a valid image file!"); return; } <BR>oFReader.readAsDataURL(aFiles[0]); <BR>} <br><br>} <BR>if (navigator.appName === "Microsoft Internet Explorer") { <BR>return function () { <BR>alert(document.getElementById("imageInput").value); <BR>document.getElementById("imagePreview").filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = document.getElementById("imageInput").value; <br><br>} <BR>} <BR>})(); <BR> <BR>#imagePreview { <BR>width: 160px; <BR>height: 120px; <BR>filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale); <BR>} <BR> 但是需要注意的是 在IE8中 由于IE8自作聪明的将真实路径隐藏起来了 用alert打印的结果是C:/fakepath/*.jpg 所以导致该方法无法使用 解决办法是:进入工具 -> Internet选项 -> 安全 -> 自定义级别 -> 找到“其他”中的“将本地文件上载至服务器时包含本地目录路径”,选中“启用”即可。