亲宝软件园·资讯

展开

jquery实现上传图片功能

qq_42221334 人气:1

本文着重为大家仔细讲解了jquery实现上传图片功能,文中代码实例讲解的非常细致,希望能够帮助到您,欢迎大家阅读和收藏

代码:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>点击头像上传图片</title>
<style>
*{
 margin:0;
 padding: 0;
}
div,#avarimgs,#xdaTanFileImg{
 width: 100px;
 height: 100px;
}
div{
 margin:50px auto;
 position: relative;
}
#xdaTanFileImg{
 position: absolute;
 top: 0;
 left: 0;
 opacity: 0;
}
</style>
</head>
<body>
 <div>
 <input type="file" name="pclogo" id="xdaTanFileImg" onchange="xmTanUploadImg(this)" accept="image/*">
 <img src="/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3787285033,4172246344&fm=26&gp=0.jpg" class="img-circle img-thumbnail img-responsive" id="avarimgs">
 </div>
</body>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript"> 
 
if (typeof FileReader == 'undefined') {
 document.getElementById("xmTanDiv").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>";
 document.getElementById("xdaTanFileImg").setAttribute("disabled", "disabled");
}
//选择图片,马上预览
 function xmTanUploadImg(obj) {
 console.log(obj)
 var file = obj.files[0];  
 console.log(obj);console.log(file);
 console.log("file.size = " + file.size);
 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("avarimgs");
 img.src = e.target.result;
 //或者 img.src = this.result; //e.target == this
 }
 reader.readAsDataURL(file)
 }
</script>
 
</html>

加载全部内容

相关教程
猜你喜欢
用户评论