<input id="file" type="file">
<img id="img" >
<script>
var file = document.getElementById(‘file‘)
var img = document.getElementById(‘img‘)
file.addEventListener(‘change‘,function(){
var obj = file.files[0]
var reader = new FileReader();
reader.readAsDataURL(obj);
reader.onloadend = function() {
img.setAttribute(‘src‘,reader.result);
}
})
</script>
<input id="file" type="file">
<img id="img" >
<script>
var file = document.getElementById(‘file‘)
var img = document.getElementById(‘img‘)
file.addEventListener(‘change‘,function(){
var obj = file.files[0]
var src = window.URL.createObjectURL(obj)
img.setAttribute(‘src‘,src);
})
</script>
原文地址:http://blog.51cto.com/12173069/2126324