你的位置:首页 > Java教程

[Java教程]js图片预览

 1  2  3 <!DOCTYPE html> 4 <html> 5 <head> 6 <title></title> 7 </head> 8 <body> 9 <div id="divPreview">10 <img id="imgHeadPhoto" src="noperson.jpg" style="width: 160px; height: 170px; border: solid 1px #d2e2e2;"11 alt="" />12 </div>13 <input type="file" onchange="PreviewImage(this,'imgHeadPhoto','divPreview');" size="20" />14 <script type="text/javascript">15 //js本地图片预览,兼容ie[6-9]、火狐、Chrome17+、Opera11+、Maxthon316 function PreviewImage(fileObj, imgPreviewId, divPreviewId) {17 var allowExtention = ".jpg,.bmp,.gif,.png"; //允许上传文件的后缀名document.getElementById("hfAllowPicSuffix").value;18 var extention = fileObj.value.substring(fileObj.value.lastIndexOf(".") + 1).toLowerCase();19 var browserVersion = window.navigator.userAgent.toUpperCase();20 if (allowExtention.indexOf(extention) > -1) {21 if (fileObj.files) {//HTML5实现预览,兼容chrome、火狐7+等22 if (window.FileReader) {23 var reader = new FileReader();24 reader.onload = function (e) {25 document.getElementById(imgPreviewId).setAttribute("src", e.target.result);26 }27 reader.readAsDataURL(fileObj.files[0]);28 } else if (browserVersion.indexOf("SAFARI") > -1) {29 alert("不支持Safari6.0以下浏览器的图片预览!");30 }31 } else if (browserVersion.indexOf("MSIE") > -1) {32 if (browserVersion.indexOf("MSIE 6") > -1) {//ie633 document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);34 } else {//ie[7-9]35 fileObj.select();36 if (browserVersion.indexOf("MSIE 9") > -1)37 fileObj.blur(); //不加上document.selection.createRange().text在ie9会拒绝访问38 var newPreview = document.getElementById(divPreviewId + "New");39 if (newPreview == null) {40 newPreview = document.createElement("div");41 newPreview.setAttribute("id", divPreviewId + "New");42 newPreview.style.width = document.getElementById(imgPreviewId).width + "px";43 newPreview.style.height = document.getElementById(imgPreviewId).height + "px";44 newPreview.style.border = "solid 1px #d2e2e2";45 }46 newPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + document.selection.createRange().text + "')";47 var tempDivPreview = document.getElementById(divPreviewId);48 tempDivPreview.parentNode.insertBefore(newPreview, tempDivPreview);49 tempDivPreview.style.display = "none";50 }51 } else if (browserVersion.indexOf("FIREFOX") > -1) {//firefox52 var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(/firefox\/([\d.]+)/)[1]);53 if (firefoxVersion < 7) {//firefox7以下版本54 document.getElementById(imgPreviewId).setAttribute("src", fileObj.files[0].getAsDataURL());55 } else {//firefox7.0+ 56 document.getElementById(imgPreviewId).setAttribute("src", window.URL.createObjectURL(fileObj.files[0]));57 }58 } else {59 document.getElementById(imgPreviewId).setAttribute("src", fileObj.value);60 }61 } else {62 alert("仅支持" + allowExtention + "为后缀名的文件!");63 fileObj.value = ""; //清空选中文件64 if (browserVersion.indexOf("MSIE") > -1) {65 fileObj.select();66 document.selection.clear();67 }68 fileObj.outerHTML = fileObj.outerHTML;69 }70 return fileObj.value; //返回路径71 }72 </script>73 </body>74 </html>