<script> function getObjectURL(file) { var url = null; if (window.createObjectURL != undefined) { // basic url = window.createObjectURL(file); } else if (window.URL != undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file); } else if (window.webkitURL != undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file); } return url; } function previewimg(file, imgTag) { var objUrl = getObjectURL(document.getElementById(file).files[0]); if (objUrl) { document.getElementById(imgTag).src = objUrl; } } </script>
使用
<img id="myimg1" src="" style="height:90px;" /> <input id="uploadimg1" type="file" name="img1" value="" onchange="previewimg('uploadimg1', 'myimg1');" />