<div class="codetitle"> <span><a style="CURSOR: pointer" data="13100" class="copybut" id="copybut13100" onclick="doCopy('code13100')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code13100"> <br><div id="localImag"><img id="preview" style="max-width:90%" height="-1" style="display:none" alt="javascript上传图片前预览图片兼容大多数浏览器_javascript技巧" ></div> <br><fileupload id="file_head" runat="server" onchange="javascript:setImagePreview();"></fileupload> <br> </div> <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="4814" class="copybut" id="copybut4814" onclick="doCopy('code4814')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code4814"> <br><script type="text/javascript"> <BR>function setImagePreview() { <BR>var docObj = document.getElementById("ctl00_ContentMain_file_head"); <BR>var fileName = docObj.value; <BR>if (!fileName.match(/.jpg|.gif|.png|.bmp/i)) { <BR>alert('您上传的图片格式不正确,请重新选择!'); <BR>return false; <BR>} <br><br>var imgObjPreview = document.getElementById("preview"); <BR>if (docObj.files && docObj.files[0]) { <BR>//火狐下,直接设img属性 <BR>imgObjPreview.style.display = 'block'; <BR>imgObjPreview.style.width = '63px'; <BR>imgObjPreview.style.height = '63px'; <BR>//imgObjPreview.src = docObj.files[0].getAsDataURL(); <BR>if (window.navigator.userAgent.indexOf("Chrome") >= 1 || window.navigator.userAgent.indexOf("Safari") >= 1) { <BR>imgObjPreview.src = window.webkitURL.createObjectURL(docObj.files[0]); <BR>} <BR>else { <BR>imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]); <BR>} <BR>} else { <BR>//IE下,使用滤镜 <BR>docObj.select(); <BR>docObj.blur(); <BR>var imgSrc = document.selection.createRange().text; <BR>var localImagId = document.getElementById("localImag"); <BR>//必须设置初始大小 <BR>localImagId.style.width = "63px"; <BR>localImagId.style.height = "63px"; <BR>//图片异常的捕捉,防止用户修改后缀来伪造图片 <BR>try { <BR>localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; <BR>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><br></script> <br> </div> <br>此js解决360浏览器显示图片问题。因为360浏览器6.2用的是Chrome的内核,而 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="82788" class="copybut" id="copybut82788" onclick="doCopy('code82788')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code82788"> <br>window.URL.createObjectURL(docObj.files[0]); <br> </div> <br>只有在火狐下才认得到,chrome中不认识window.URL.createObjectURL,所有用Safari的window.webkitURL.createObjectURL