js上传图片并展示

<style>
    .file {

        border: 1px solid rgba(0, 0, 0, 0.22);
        position: relative;
        display: inline-block;
        border-radius: 4px;
        padding: 4px 12px;
        overflow: hidden;
        color: #1E88C7;
        text-decoration: none;
        text-indent: 0;
        line-height: 20px;
        width: 16%;
        height: 220px;
        background: url(/img/default.png);
        background-size: 100% 100%;
        background-position: center center;
    }

    .file input {
        cursor: pointer;
        border-color: #78C3F3;
        position: absolute;
        font-size: 100px;
        right: 0;
        top: 0;
        opacity: 0;
        width: 100%;
        height: 100%;
    }

    .file:hover {
        border-color: #78C3F3;
        color: #004974;
        text-decoration: none;
    }
</style>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js上传图片并展示</title>
</head>
<body>
    <a href="javascript:;" class="file">
        <input type="hidden" name="mec_share_img" value="" class="mec_img">
        <input type="file" name="share_img" class="img" kb_max="1024" data-format="image/gif,image/jpeg,image/png,image/jpg,image/bmp">
    </a>
</body>
</html>
<script src="/js/jquery.min.js"></script>
<script src="/layui/layui.all.js"></script>
<script>
    $(".img").change(function () {

        var reads = new FileReader();
        var f = $(this).get(0).files[0];
        var filesize = f.size;
        var maxsize = $(this).attr('kb_max');//1M
        var kb_max = maxsize + 'K';
        if(maxsize >= 1024){
            kb_max = maxsize / 1024 + 'M';
        }
        if ((filesize/1024) > maxsize) {
            layer.msg('请上传小于'+kb_max+'的图片!');return ;
        }
        reads.readAsDataURL(f);
        var that = this;
        reads.onload = function (e) {
            var _this = this
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var image = new Image();
            image.src = data;
            // 图片先加载完,才可以得到图片宽度和高度
            image.onload = function () {
                $(that).parents('.file').css("background","url("+ _this.result+")")
                $(that).parents('.file').css("background-size","100% 100%")
                $(that).parents('.file').css(" background-position","center center")
            }
        };

        // var formData = new FormData();
        // formData.append("upload_file",f);
        // var url = '/system/ajax/ajax_upload_image';
        // $.ajax({
        //     url:url, /*接口域名地址*/
        //     type:'post',
        //     data: formData,
        //     contentType: false,
        //     processData: false,
        //     success:function(res){
        //         $(that).parents('.file').find('.mec_img').val(res.data.save_name)
        //     }
        // })
    })
</script>```
![url图片](https://img-blog.csdnimg.cn/20210117133606715.png#pic_center)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值