标签:
1 <div> 2 <%-- 核心思想:把FileUpload设为relative,top:-200px;opacity:0 --%> 3 <div id="localImag" style="width: 300px; height: 200px"> 4 <img id="preview" alt="预览图片" src="images/qiye.png" width="300" height="200" /> 5 <asp:FileUpload ID="PicLoad" Width="300px" Height="200px" Style="position: relative; top: -200px; opacity: 0" runat="server" onchange="javascript:setImagePreview(this,localImag,preview);"></asp:FileUpload> 6 7 </div> 8 <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="上传" /> 9 10 <script type="text/javascript"> 11 function setImagePreview(docObj, localImagId, imgObjPreview) { 12 if (docObj.files && docObj.files[0]) { 13 //火狐下,直接设img属性 14 imgObjPreview.style.display = ‘block‘; 15 imgObjPreview.style.width = ‘300px‘; 16 imgObjPreview.style.height = ‘200px‘; 17 18 19 //火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式 20 imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]); 21 } 22 else { 23 //IE下,使用滤镜 24 docObj.select(); 25 var imgSrc = document.selection.createRange().text; 26 27 //必须设置初始大小 28 localImagId.style.width = "300px"; 29 localImagId.style.height = "200px"; 30 31 //图片异常的捕捉,防止用户修改后缀来伪造图片 32 try { 33 localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; 34 localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc; 35 } 36 catch (e) { 37 alert("您上传的图片格式不正确,请重新选择!"); 38 return false; 39 } 40 imgObjPreview.style.display = ‘none‘; 41 document.selection.empty(); 42 } 43 return true; 44 } 45 </script> 46 </div>
webform 文件上传(头像上传) 隐藏FileUpload
标签:
原文地址:http://www.cnblogs.com/xtxtx/p/4877655.html