0

I have a change event that enable me to upload images.

<input type="file" @change="onFileChange" multiple>

   const onFileChange = (e) => {
      for (let i = 0; i < e.target.files.length; i++) {
        const file = e.target.files[i]
        imageList.value.push(file)
        localUrls.value.push(URL.createObjectURL(file))
      }

My problem is after I add image 1 2 and 3. Then proceed to delete image 1 2 and 3. And when I try to add image 1 2 and 3 it will not work because I realize that my change event handler thinks that nothing has changed because when I add image 1 2 and 3 again, it remembers when I added image 1 2 and 3 previously and thus does not update.

Is there a way for me to reset my change event handler to forget its previous values that it was holding? Thanks!

The complete version of my code is below

<template>
  <h2>Add your product</h2>
  <form @submit.prevent="handleSubmit">
    <label>Product Name</label>
    <input type="text" placeholder="Name of Product" v-model="productName">
    <label>Quantity</label>
    <input type="number" placeholder="Quantity" v-model="productQuantity">
    <label>Description</label>
    <textarea placeholder="Description" v-model="productDescription"></textarea>
    <label>Tags</label>
    <input type="text" placeholder="tags, to be changed" v-model="productTags">
    <label>Photos</label>
    <input type="file" @change="onFileChange" multiple>
    <div class="container">
      <div v-for="image in localUrls" :key="image">
        <ProductImage :image="image" @removeImage="removeImage" />
      </div>
    </div>
    <p v-if="error">{{ error }}</p>
    <button>Submit</button>
  </form>
</template>

<script>
import { ref } from '@vue/reactivity'
import AddProduct from "../../composables/addProduct.js"
import { timestamp } from '../../firebase/config.js'
import ProductImage from './ProductImage.vue'

export default {
  components: {
    ProductImage
  },
  setup() {
    // Handling Images
    const imageList = ref([]);
    const localUrls = ref([]);

    const removeImage = (imageForRemoval) => {
      const index = localUrls.value.findIndex(el => (el == imageForRemoval))
      imageList.value.splice(index, 1)
      localUrls.value.splice(index, 1)
    }

    const onFileChange = (e) => {
      for (let i = 0; i < e.target.files.length; i++) {
        const file = e.target.files[i]
        imageList.value.push(file)
        localUrls.value.push(URL.createObjectURL(file))
      }
    }

    const productName = ref('')
    const productQuantity = ref('')
    const productDescription = ref('')
    const productTags = ref('')
    const { uploadProduct, error } = AddProduct();

    // Uploading Data in Field to Database
    const handleSubmit = async() => {
      var productData = {
        product: productName.value, 
        productQuantity: productQuantity.value, 
        productDescription: productDescription.value,
        productTags: productTags.value,
        createdAt: timestamp
      };
      await uploadProduct(productData)
      if (error.value == null) {
        productName.value = ''
        productQuantity.value = ''
        productDescription.value = ''
        productTags.value = ''
      }
    }
    
    return {
      productName,
      productQuantity,
      productDescription,
      productTags,
      handleSubmit,
      error,
      onFileChange,
      localUrls,
      removeImage,
    }
  }


}
</script>
tan shi yu
  • 303
  • 1
  • 7
  • 1
    Put `imageList.value = [];` at the beginning of the function. – Barmar Jun 08 '21 at 15:27
  • Hello, I tried the method you have mentioned that it does not seem to work. When I delete my value, my imageList also updates. I think my event handler specifically remembers the values it previously held. Is there a way to reset the event handler? Thanks! – tan shi yu Jun 08 '21 at 15:31
  • You're also saving in `localUrls.value` you need to reset that as well. – Barmar Jun 08 '21 at 15:34
  • Nothing is saved automatically by the event handler. Just the variables you assign in the event handler. – Barmar Jun 08 '21 at 15:35
  • I think it does remember as my onFileChange does not run at all if the sequence of files that I put in there before is the same as the sequence of file I put in there next. I can put file 1 2 and 3. I put in file 1 2 and 3 again without deleting the file 1 2 and 3 that is already in there. I will only log out 1 set of 1 2 and 3 because my onFileChange function does not run because the @change function does not trigger. – tan shi yu Jun 08 '21 at 15:45
  • But yeah it is wrong to say onFileChange remembers anything because it is a function. I think it's the change listener that remembers what value it held previously. – tan shi yu Jun 08 '21 at 15:46
  • After every change set `input.value = ''`, in your case, add this to the end of your event handler `e.target.value = ''` – Anonymous Jun 08 '21 at 15:48
  • @Anonymous It's a file input, not a text input. – Barmar Jun 08 '21 at 15:52
  • @tanshiyu Event listeners don't remember anything. Your code is remembering things in the global variables `imageList` and `localUrls`. – Barmar Jun 08 '21 at 15:52
  • @Barmar Doesn't matter https://stackoverflow.com/a/16222877/437393 if it does, replace empty string with NULL, https://stackoverflow.com/a/21155838/437393 – Anonymous Jun 08 '21 at 15:55
  • @Anonymous Anyway, I don't think they want to clear the file input after every change, since that will make it impossible to select multiple files. – Barmar Jun 08 '21 at 15:57
  • What is `@change="onFileChange"`? That should be `onchange="onFileChange()"` – Barmar Jun 08 '21 at 15:58
  • Is that part of a framework? Maybe that's saving things. – Barmar Jun 08 '21 at 15:59
  • Yeah it is part of vue.js framework. Sorry I thought I didn't mention this because I was under the impression that the problem lies in the change event listener. Lemme post the entire code here as well. – tan shi yu Jun 08 '21 at 16:07
  • 1
    Hello all, I have found an answer that works for me and it is detailed here: https://stackoverflow.com/questions/4109276/how-to-detect-input-type-file-change-for-the-same-file There is a solution for vue.js and a lot of other stuff. But yeah, thanks for helping out everyone who tried. I truly appreciate it! – tan shi yu Jun 08 '21 at 17:44
  • https://stackoverflow.com/questions/4109276/how-to-detect-input-type-file-change-for-the-same-file – tan shi yu Sep 27 '21 at 07:45

0 Answers0