在页面上选择图片的时候显示选择的图片文件以及自动生成图片缩略图

页面代码

<div class="form-group">
                    <label class="col-sm-1 control-label">上传图片</label> 
                    <input type="file" id="file1" name="file1" onchange="readFile()"><span style="color: red">注:图片大小最大不能超过1M!</span>
    <div class="col-sm-11">
    </div>
                    <img src="" id="img-id" onload="ReSizePic(this)" />
</div>

js代码

//回显图片
function readFile(){
    //得到文件对象
    var imgFile=document.getElementById('file1').files[0];
    //创建filereader对象
    var fileReader = new FileReader();
    //设置回调函数,即获取到文件路径后的操作
    fileReader.onload=function(){
        var img =document.getElementById('img-id').src=fileReader.result;
    }
    //读取文件
    fileReader.readAsDataURL(imgFile);
};
//设置自动生成图片缩略图
function ReSizePic(ThisPic){
    var RePicWidth = 200; //这里修改为您想显示的宽度值

    //============以下代码请勿修改==================================

    var TrueWidth = ThisPic.width;    //图片实际宽度
    var TrueHeight = ThisPic.height;  //图片实际高度
    var Multiple = TrueWidth / RePicWidth;  //图片缩小(放大)的倍数

    ThisPic.width = RePicWidth;  //图片显示的可视宽度
    ThisPic.height = TrueHeight / Multiple;  //图片显示的可视高度
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值