js 上传图片预览

 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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值