• vue头像上传


    项目四知识点

    • 默认头像

    • 选择头像

    <template>
        <div class="adatar">
            <img :src="adatar?adatar:require('../../assets/images/default-user.png')" alt="" >
            <input type="file" name="" accept="image/gif,image/jpeg,image/jpg,image/png" @change="fileChange">
            <button @click="submit">提交</button>
        </div>
    </template>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
    export default {
        name: 'adatar',
        data() {
            return {
                adatar: ''
            }
        },
        methods: {
            //头像选择
            fileChange(e) {
                var that = this;
                var file = e.target.files[0];
                var reader = new FileReader();
                reader.onload = function(e){
                    that.adatar  = e.target.result;
                }
                reader.readAsDataURL(file);
            },
            submit() {
                var data = new FormData();
                data.appendTo('file',this.adatar);
                $ajax({
                    type: 'POST',
                    data: data,
                    processData: false, //processData 默认为false,当设置为true的时候,jquery ajax 提交的时候不会序列化 data,而是直接使用data
                    contentType: false,
                    success:function(res){
                    },
                    error:function(err){
                    }
                })
            }
        }
    }
    </script>
    <style lang="less">
        .adatar {
            position: relative;
             168px;
            height: 168px;
            img {
                object-fit: cover;
                object-position: center;
                 100%;
                height: 100%;
                border-radius: 50%;
            }
            input {
                position: absolute;
                top: 0;
                right: 0;
                 100%;
                height: 100%;
                border-radius: 50%;
                outline: none;
                opacity: 0;
                cursor: pointer;
                &:focus {
                    box-shadow: none;
                }
            }
            button {
                 100%;
                height: 30px;
                margin-top: 20px;
                text-align: center;
                border: 1px solid #ccc;
                color: #fff;
                font-size: 14px;
                background: pink;
            }
    
        }
    
    </style>
  • 相关阅读:
    Redis 分布式锁
    Angular VS Blzaor
    Chorme 跨域的快捷解决
    旋转3角形
    .Netcore AD 操作
    .Netcore 2.2 和3.1 的模板
    Command3
    CSS Selector
    弹性盒子
    Label_strange_labels
  • 原文地址:https://www.cnblogs.com/DCL1314/p/10025702.html
Copyright © 2020-2023  润新知