19

How can we file input detect change on SAME file input in Vue Js

<input ref="imageUploader" type="file" @change="uploadImageFile">

3
  • 1
    Possible duplicate of How to detect input type=file "change" for the same file? Commented Jan 10, 2019 at 9:06
  • It's not a change if it's the same file. One option would be clear the field when the input is clicked so that when they select a file it's always a change. Commented Jan 10, 2019 at 9:07
  • 1
    Not a duplicate of that question since that is in jquery and this is in vuejs. Both have different ways to access the fields. You are right about clearing the field on click because I wanna trigger the change event every time a file is uploaded Commented Jan 10, 2019 at 9:42

5 Answers 5

40

We can add @click event and then clear the value of the file input

<template>
    ....
        <input ref="imageUploader" type="file" accept=".jpg, .jpeg" @click="resetImageUploader" @change="uploadImageFile">
    ....
</template>

<script>
    export default {
        methods: {
            resetImageUploader() {
                this.$refs.imageUploader.value = '';
            },
            uploadImageFile() {
                ....
            }
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

even easier - put the this.$refs.imageUploader.value = ''; to the end of the uploadImageFile method :)
7

Same thing as @zubair's answer but for vue 3 and a bit more convenient:

<input type="file" @change="renderImage($event)" @click="$event.target.value=''">

Comments

1

The @zubair-0 and @grreeenn's answers are totally valid, Here you can have an implementation initializing the input value with an empty string after the uploaded file is processed because the event only is fired when the value changed, you can do this in Vue 3 using the Template Refs.

<template>
  <input
      ref="imageUploader"
      type="file"
      class="custom-file-input"
      name="file-upload"
      accept="image/png, image/gif, image/jpeg"
      @change="uploadImageFile($event)"
    >
</template>
<script>
  import { ref } from 'vue'
  export default {
     setup() {
       const imageUploader = ref(null)
       const uploadImageFile = (event) => {
          console.log('File loaded...')
          // We initialize the input value, this is the trick
          imageUploader.value.value = ''
       }
       return {
         imageUploader,
         uploadImageFile
       }
   }
 }
</script>

Comments

1

If someone ever face the same issue using Vue 3, I managed to fix it by adding the event management directly in the @click of the <input> : @click="$event.target.value = ''"

<input slot="input" type="file" @change="doSomething($event)" @click="$event.target.value = ''"/>

1 Comment

Great. But it is better to write $event.target.value = '' inside function rather then in input
0

I fix it by key. For exampleenter image description here

I hope this helps you

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.