<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_19386"> <div class="blockcode"> <div id="code_P27"><ol> <li> </li> <li> </li> <li> </li> <li> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </li> <li> <title>Firefox3,IE6,IE7,IE8上传图片预览</title> </li> <li> <style type="text/css"> <li>#preview_wrapper{ <li> display:inline-block; <li> width:300px; <li> height:300px; <li> background-color:#CCC; <li>} <li>#preview_fake{ /* 该对象用户在IE下显示预览图片 */ <li> filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale); <li>} <li>#preview_size_fake{ /* 该对象只用来在IE下获得图片的原始尺寸,无其它用途 */ <li> filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image); <li> visibility:hidden; <li>} <li>#preview{ /* 该对象用户在FF下显示预览图片 */ <li> width:300px; <li> height:300px; <li>} <li></style> </li> <li> <li> <script type="text/javascript"> <li>function onUploadImgChange(sender){ <li> if( !sender.value.match( /.jpg|.gif|.png|.bmp/i ) ){ <li> alert('图片格式无效!'); <li> return false; <li> } <li> <li> var objPreview = document.getElementById( 'preview' ); <li> var objPreviewFake = document.getElementById( 'preview_fake' ); <li> var objPreviewSizeFake = document.getElementById( 'preview_size_fake' ); <li> <li> if( sender.files && sender.files[0] ){ <li> objPreview.style.display = 'block'; <li> objPreview.style.width = 'auto'; <li> objPreview.style.height = 'auto'; <li> <li> // Firefox 因安全性问题已无法直接通过 input[file].value 获取完整的文件路径 <li> objPreview.src = sender.files[0].getAsDataURL(); <li> }else if( objPreviewFake.filters ){ <li> // IE7,IE8 在设置本地图片地址为 img.src 时出现莫名其妙的后果 <li> //(相同环境有时能显示,有时不显示),因此只能用滤镜来解决 <li> <li> // IE7, IE8因安全性问题已无法直接通过 input[file].value 获取完整的文件路径 <li> sender.select(); <li> var imgSrc = document.selection.createRange().text; <li> <li> objPreviewFake.filters.item( <li> 'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc; <li> objPreviewSizeFake.filters.item( <li> 'DXImageTransform.Microsoft.AlphaImageLoader').src = imgSrc; <li> <li> autoSizePreview( objPreviewFake, <li> objPreviewSizeFake.offsetWidth, objPreviewSizeFake.offsetHeight ); <li> objPreview.style.display = 'none'; <li> } <li>} <li><li>function onPreviewLoad(sender){ <li> autoSizePreview( sender, sender.offsetWidth, sender.offsetHeight ); <li>} <li><li>function autoSizePreview( objPre, originalWidth, originalHeight ){ <li> var zoomParam = clacImgZoomParam( 300, 300, originalWidth, originalHeight ); <li> objPre.style.width = zoomParam.width + 'px'; <li> objPre.style.height = zoomParam.height + 'px'; <li> objPre.style.marginTop = zoomParam.top + 'px'; <li> objPre.style.marginLeft = zoomParam.left + 'px'; <li>} <li><li>function clacImgZoomParam( maxWidth, maxHeight, width, height ){ <li> var param = { width:width, height:height, top:0, left:0 }; <li> <li> if( width>maxWidth || height>maxHeight ){ <li> rateWidth = width / maxWidth; <li> rateHeight = height / maxHeight; <li> <li> if( rateWidth > rateHeight ){ <li> param.width = maxWidth; <li> param.height = height / rateWidth; <li> }else{ <li> param.width = width / rateHeight; <li> param.height = maxHeight; <li> } <li> } <li> <li> param.left = (maxWidth - param.width) / 2; <li> param.top = (maxHeight - param.height) / 2; <li> <li> return param; <li>} <li></script> </li> <li> <li> </li> <li> <li> </li> <li> <div id="preview_wrapper"> <li> <div id="preview_fake"> <li> <img id="preview" onload="onPreviewLoad(this)" alt="js 上传图片前,预览图片。。。兼容 IE 6 IE7 IE8 、 火狐。。。 " > </li> <li> </li> </div> </li> <li> </li> </div> </li> <li> <br> </li> <li> <input id="upload_img" type="file" onchange="onUploadImgChange(this)"> </li> <li> <br> </li> <li> <img id="preview_size_fake" alt="js 上传图片前,预览图片。。。兼容 IE 6 IE7 IE8 、 火狐。。。 " > </li> <li> </li> <li> <li> </ol></div> <em onclick="copycode($('code_P27'));">复制代码</em> </div> </td></tr></table> <div id="comment_19386" class="cm"> </div> <div id="post_rate_div_19386"></div> <br><br>