页面上传图片及时显示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<input type=file name="fileupload" id="fileupload" οnchange="javascript:setImgView('fileupload','preview');">
<p>
<div id="localImag">
<img id="preview"/>
</div>
</p>
<script>
function setImgView(fileId,imgId){
	var obj = document.getElementById(fileId);
	if(/msie/.test(navigator.userAgent.toLowerCase())){  
		document.getElementById(imgId).src = obj.value;
	}else{  
	    var objUrl = getObjectURL(obj.files[0]);  
	    if(objUrl){  
	    	document.getElementById(imgId).src = objUrl;  
	    }
	}  
}
//建立一个可存取到该file的url  
function getObjectURL(file) {  
	var url = null ;   
	if (window.createObjectURL != undefined) {  
	    url = window.createObjectURL(file) ;  
	}else if (window.URL!= undefined) {   
	     url = window.URL.createObjectURL(file) ;  
	}else if (window.webkitURL != undefined) {   
	     url = window.webkitURL.createObjectURL(file) ;  
	}  
	return url ;  
} 
</script>
</body>
</html>

发布了233 篇原创文章 · 获赞 31 · 访问量 11万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章