2

I'm using the Bootstrap-Vue component in a Vue-project with the accept prop set to "image/*". It works fine with the Browse-Button, but i can still drop all kind of files to the file-form and it accepts it. Is this a bug or am i missing something?

<b-form-file
   accept="image/*"
   v-model="file"
   :state="null"
   placeholder="Choose a file or drop it here..."
   drop-placeholder="Drop file here..."
></b-form-file> 

1 Answer 1

5

Bootstrap-Vue's <b-form-file> component, is a wrapper for a native <input type="file"> element. Where the input is not visible and instead shows a customized label, but still utilizes the input for file handling.

That means it will function the same way as the native input, which allows anything to be dropped into it. Even when having accept="image/*" applied. (at least in Chrome)

You will probably need to do some manual validation when your v-model changes to make sure it's in a correct format.

You could for example create a watcher to reset the input if it's an invalid file.

watch: {
  file(newFile) {
    if(newFile && !newFile.type.startsWith("image/")) {
      this.$nextTick(() => {
        this.file = null;        
      })
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Hiws -- this worked very well, thanks for your explanation and the code! Very much appreciated!
thank you as well. Was just looking for some kind of validation to now allow anything else besides csv.

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.