参考了网上一些人代码写了一个上传图片及时预览的功能
<img id="imgTag" style="height: 100px;" alt="" /><input type="file" />
function DisplayImage(fileTag,imgTagId){ var allowExtention=".jpg.png.gif";var extentionArr=fileTag.value.split('.');var extention = extentionArr[extentionArr.length-1];if(!(allowExtention.indexOf(extention)>-1)){ alert("Please upload image!");}else{ //for adveced broswer(the newest ie,chrome,ff)if(typeof(FileReader)!=="undefined"){ var reader = new FileReader();reader.readAsDataURL(fileTag.files[0]);reader.onload = function(e){ document.getElementById(imgTagId).setAttribute("src", e.target.result);}}else{ //for(ie6)document.getElementById(imgTagId).setAttribute("src",fileTag.value);}}}
学习来源: