HTML
<ul class="gjimg">
<li>
<b>正面</b><span onclick="zhDel()">删除</span>
<a href="javascript:;" class="afile">
<input type="file" name="zheng" id="xdaTanFileImg" onchange="xmTanUploadImg(this)" accept="image/*" >点击上传正面图
</a>
<img id="xmTanImg"/>
<div id="xmTanDiv"></div>
<div class="clear"></div>
</li>
</ul>
CSS
.gjimg li{
float:left;
width:265px;
margin-left:100px;
height:283px;
overflow:hidden;
}
.gjimg li span{
font-size:14px;
color:#000;
margin-left:65px;
cursor:pointer;
}
.gjimg li .afile{
width:230px;
height: 230px;
line-height: 230px;
position: relative;
cursor: pointer;
color: #000;
font-size:16px;
text-align:center;
background: #e5e5e5;
border: 1px solid #d4d4d4;
border-radius: 1px;
overflow: hidden;
display: inline-block;
*display: inline;
*zoom: 1
}
.gjimg li .afile input{
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
filter: alpha(opacity=0);
cursor: pointer;
width:230px;
height: 230px;
}
#xmTanImg{
width:232px;
height:232px;
position:relative;
top:-232px;display:none;
}
if (typeof FileReader == 'undefined') {
document.getElementById("xmTanDiv").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>";
//使选择控件不可操作
document.getElementById("xdaTanFileImg").setAttribute("disabled", "disabled");
}
function xmTanUploadImg(obj) {
var file = obj.files[0];
console.log(obj);console.log(file);
console.log("file.size = " + file.size); //file.size 单位为byte
var reader = new FileReader();
//读取文件过程方法
reader.onloadstart = function (e) {
console.log("开始读取....");
}
reader.onprogress = function (e) {
console.log("正在读取中....");
}
reader.onabort = function (e) {
console.log("中断读取....");
}
reader.onerror = function (e) {
console.log("读取异常....");
}
reader.onload = function (e) {
console.log("成功读取....");
var img = document.getElementById("xmTanImg");
img.style="display:block;";
img.src = e.target.result;
//或者 img.src = this.result; //e.target == this
}
reader.readAsDataURL(file);
}