3

Helo. i want to check my input file type if it's jpg or not. i worked with 'vee-validate', which it was fine, but what i like to do is like what I've did with file size base on a tutorial.

Here's the code:

<input type="file" @change="updateMelliCodeFrontScan" name="mellicode_front_url" class="form-input" >

Here's the Vue method:

updateMelliCodeFrontScan(e){
            // console.log('uploading');
            let file = e.target.files[0];
            let reader = new FileReader();
            // let vm = this;
            if (file['size'] < 200000){

                    reader.onloadend = (file) => {
                        // console.log('RESULT', reader.result)
                        this.form.mellicode_front_url = reader.result;
                    }
                    reader.readAsDataURL(file);
            }else
            {
                swal({
                    type: 'error',
                    title: 'Size limited.',
                    text: 'size limit',
                })
            }
},

so i want to do it like this:

=> i want to make another if for file type base on extension/type like file['type']. 

i used file['type'] === jpg and didn't worked.
if (file['size'] < 200000){

                    reader.onloadend = (file) => {
                        // console.log('RESULT', reader.result)
                        this.form.mellicode_front_url = reader.result;
                    }
                    reader.readAsDataURL(file);
            }else
            {
                swal({
                    type: 'error',
                    title: 'Size limited.',
                    text: 'size limit',
                })
}

2 Answers 2

10

It's because it return image/jpeg in file[0]

if(files[0]['type']==='image/jpeg')

Try this it's works.

Sign up to request clarification or add additional context in comments.

2 Comments

how about adding more types? i've tried === 'image/jpeg' || 'image/png'), didn't worked.
Try this files[0]['type']==='image/jpeg' || files[0]['type']==='image/png'
2

For multiple file type checking you can use like this

if(['image/png','image/jpeg','image/svg'].includes(files[0]['type'])){
   
}

Comments

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.