«

Element UI头像上传组件自定义格式上传

时间:2023-6-8 00:02     作者:六思逸     分类: Vue


当我们在使用Element UI 头像上传组件时,它默认只能上传jpg格式的图片,我们怎么添加自己想要的格式呢?

Element UI 原代码

beforeAvatarUpload(file) {
    const isJPG = file.type === 'image/jpeg';
    const isLt2M = file.size / 1024 / 1024 < 2;

    if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
    }
    if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
    }
    return isJPG && isLt2M;
}

修改后代码

beforeUpload(file) {
    // MIME: 多种格式的邮件扩展  文件类型的描述
    let mime = ["image/jpeg", "image/png", "image/gif"]; //允许上传三种格式的图片
    const isJPG = mime.includes(file.type);
    const isLt2M = file.size / 1024 / 1024 < 2;

    if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG、PNG、GIF 格式!");
    }
    if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
    }
    return isJPG && isLt2M;
},

标签: 前端开发 Element UI 头像上传 自定义格式

版权所有:六思逸
文章标题:Element UI头像上传组件自定义格式上传
除非注明,文章均为 六思逸 原创,转载请注明作者和出处 六思逸

扫描二维码,在手机上阅读

推荐阅读: