JS上傳圖片選擇後當即預覽

<html>
<head>
<title>測試</title>
<script type="text/javascript">
    window.onload = function () {
        if (window.attachEvent) {
            document.getElementById("file1").attachEvent('onpropertychange', function (o) {
                document.getElementById("imgID").src = 'file:///' + document.getElementById("file1").value;
            });
        }
        else {
            document.getElementById("file1").onchange = function () {
                document.getElementById("imgID").src = window.URL.createObjectURL(document.getElementById("file1").files[0]);
            }
        }
    }
</script>
</head>
<body>
<div>
      <img id="imgID" src="C:\fakepath\psb.jpg" />
</div>
<div>
       上傳:<input type="file" id="file1" />
</div>
</body>
</html>
相關文章
相關標籤/搜索