IE8 上传图片 预览 (兼容)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<form action="">
	<input type="file" multiple="true" name="imgOne" id="imgOne" onchange="preImg(this.id,'imgPre');" /> 
    <img id="imgPre" src="" width="300px" height="300px" style="display: block;" /> 
</form>
</body>
</html>
<script>
	 function getFileUrl(sourceId) { 
            var url; 
            if (navigator.userAgent.indexOf("MSIE")>=1) { // IE >6
                url = document.getElementById(sourceId).value; 
            } else if(navigator.userAgent.indexOf("Firefox")>0) { // Firefox 
                url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 
            } else if(navigator.userAgent.indexOf("Chrome")>0) { // Chrome 
                url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0)); 
            } 
            return url; 
        }

        /** 
        * 将本地图片 显示到浏览器上 
        */ 
        function preImg(sourceId, targetId) { 
            var url = getFileUrl(sourceId); 

            var imgPre = document.getElementById("imgOne"); 
            var imgPre = document.getElementById(targetId); 
            imgPre.src = url; 
            console.log(url);
        } 
</script>

猜你喜欢

转载自blog.csdn.net/gyq04551/article/details/79670425