<html>
<body>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
function changImg(e){
for (var i = 0; i < e.target.files.length; i++) {
var file = e.target.files.item(i);
//验证是否为图片,不是就跳出循环
if (!(/^image\/.*$/i.test(file.type))) {
alert("不是图片!")
continue;
}
//实例化FileReader API
var freader = new FileReader();
freader.readAsDataURL(file);
freader.onload = function(e) {
$("#image").attr("src",e.target.result); //显示图片
}
}
}
</script>
使用方式:
<form action="">
<input type="file" name="files" accept="image/*" onchange="changImg(event)" >
<img id="image" src="" height="100px",width="100px">
</form>
</body>
</html>