0

It should only allow png, jpg, jpeg, bmp file type.

How can we do that using element ui vuejs

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

    if (!isJPG) {
        this.$message.error('Avatar picture must be JPG format!');
    }

    if (!isLt2M) {
        this.$message.error('Avatar picture size can not exceed 2MB!');
    }

    return isJPG && isLt2M;
}

Does not work for me. Also it does not display me msword type in windows system. Don't know why

Please guide
Thanks

1 Answer 1

1

If you want to only allow jpg or png image types you can write the below code. You don't need to check the type of the image (explicitly).

 <input type="file" accept="image/jpeg || image/png">

In accept attribute whatever the types you mention those kind of images only user can upload. In above if you mention only jpeg or png user can upload specified type of image only.

If you only want jpg then you can write like this.

<input type="file" accept="image/jpeg"> //
Sign up to request clarification or add additional context in comments.

2 Comments

No this is not a solution this will not stop user to check validation
I have tried it. I mentioned jpg in accept attribute and try to upload png. It worked in firefox.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.