<template>
    <el-upload class="upload-class" action="https://jsonplaceholder.typicode.com/posts/"
               :file-list="fileList" :before-upload="beforeAvatarUpload">
        <el-button size="small" type="primary">{{buttonName}}</el-button>
        <div slot="tip" class="el-upload__tip">{{description}}</div>
    </el-upload>
</template>

<script>
    export default {
        name: "plUploadButton",
        props:{
            buttonName:{type:String,default:"上传"},
            description:{type:String,default:""},
            fileType:{type:String,default:""}
        },
        data(){
          return {
              fileList:[]
          }
        },
        methods:{
            beforeAvatarUpload(file) {
                let isSure = false;
                let type = file.name.split(".")[1];
                if(this.fileType){
                    isSure = this.fileType.indexOf(type) > -1;
                }
                // const isSure = file.type === this.fileType;
                // const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isSure) {
                    this.$message.error('上传文件需要是' + this.fileType + "格式！");
                }
                /*if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }*/
                // return isJPG && isLt2M;
                return isSure;
            }
        }
    }
</script>

<style scoped>

</style>
