当前位置: 首页 > 后端技术 > PHP

图片上传预览的简单实现

时间:2023-03-29 22:08:17 PHP

图片上传预览的简单实现首先:引入placeImage.js。第二:对应的id。第三:效果如下:functionchange(){varpic=document.getElementById("preview"),file=document.getElementById("f");varext=file.value.substring(file.value.lastIndexOf(".")+1).toLowerCase();//gif在IE浏览器中暂时无法显示if(ext!='png'&&ext!='jpg'&&ext!='jpeg'&&ext!='gif'){alert("图片格式必须为png或jpg或jpeg格式或gif格式!”);返回;}varisIE=navigator.userAgent.match(/MSIE/)!=null,isIE6=navigator.userAgent.match(/MSIE6.0/)!=null;如果(是IE){file.select();varreallocalpath=document.selection.createRange().text;//IE6浏览器可以直接显示图片if(isIE6){pic.src=reallocalpath;}else{//非IE6版本的IE因为安全问题不能直接设置img的src显示本地图片,可以通过过滤器实现pic.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image',src=\""+reallocalpath+"\")";//设置img的src为base64编码的透明图片取消浏览器默认图片的显示pic.src='data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';}}else{html5Reader(文件);}}functionhtml5Reader(file){varfile=file.文件[0];varreader=newFileReader();reader.readAsDataURL(文件);reader.onload=function(e){varpic=document.getElementById("预览");pic.src=this.result;}更多技术资料请关注:gzitcast