<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传-图片预览</title>
</head>
<body>
<div class="fontBt">
<p>名片正面照图片大小不超过2M</p>
<a href="javascript:;" class="file">立即上传
<input type="file" name="businessCard1" id="idDiv1">
</a>
</div>
<div id="idShowDiv1" style="width:300px;height:300px;"></div>
</body>
<script type="text/javascript">
//设置监听事件
document.getElementById('idDiv1').addEventListener('change',function(e){
var files = this.files;
var img = new Image();
var reader = new FileReader();
reader.readAsDataURL(files[0]);
reader.onload = function(e){
var mb = (e.total/1024)/1024;
if(mb>= 2){
alert('文件大小大于2M');
return;
}
img.src = this.result;
img.style.width = "100%";
img.style.height = "100%";
document.getElementById('idShowDiv1').innerHTML = '';
document.getElementById('idShowDiv1').appendChild(img);
}
});
</script>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传-图片预览</title>
</head>
<body>
<div class="fontBt">
<p>名片正面照图片大小不超过2M</p>
<a href="javascript:;" class="file">立即上传
<input type="file" name="businessCard1" id="idDiv1">
</a>
</div>
<div id="idShowDiv1" style="width:300px;height:300px;"></div>
</body>
<script type="text/javascript">
//设置监听事件
document.getElementById('idDiv1').addEventListener('change',function(e){
var files = this.files;
var img = new Image();
var reader = new FileReader();
reader.readAsDataURL(files[0]);
reader.onload = function(e){
var mb = (e.total/1024)/1024;
if(mb>= 2){
alert('文件大小大于2M');
return;
}
img.src = this.result;
img.style.width = "100%";
img.style.height = "100%";
document.getElementById('idShowDiv1').innerHTML = '';
document.getElementById('idShowDiv1').appendChild(img);
}
});
</script>
</html>