1

I am doing file upload and get the image value.

<template>
    <div class="container">
        <div class="row">
            <input type="file" name="file" @change="changeFile" />
            <input type="file" name="logo" @change="changeLogo" />
            <input type="file" name="headerImg" multiple @change="changeHeader" />
        </div>
    </div>
</template>

data () {
   return: {
     file: '',
     logo: '',
     headerImg: [],
   }
},
methods: {
    changeFile() {
      let file = e.target.files[0];
      this.file = file;
    }
    changeLogo() {
      let logo = e.target.files[0];
      this.logo = logo;
    }
    changeHeader() {
      let header = e.target.files[0];
      this.headerImg = header;
    }
}

I have three input file uploads, each input has an @change event to get the image when selected.Is there a way for those three input file uploads to use the same change method? Help me please.

2 Answers 2

2

use HTMLInputElement Name Properties

<input type="file" name="file" @change="changeFile" />
<input type="file" name="logo" @change="changeFile" />
<input type="file" name="headerImg" @change="changeFile" />

changeFile(e){
  const name = e.target.name,
        file = e.target.files[0]
  const hasName = ['file', 'logo','headerImg'].includes(name)
  if(hasName && file) this[name] = file
  else console.log('error')
}

one of multipe image upload input

data(){
  return {
    // file or files
    file: null,
    logo: null,
    headerImg: null
  }
}

<input type="file" name="file" @change="changeFile" />
<input type="file" name="logo" @change="changeFile" />
<input type="file" name="headerImg" @change="changeFile" />

changeFile(e){
  const name = e.target.name,
        files = e.target.files,
        fileLength = e.target.files.length
  const hasName = ['file', 'logo','headerImg'].includes(name)
  // fileLength: 1 or 2,3,4,5
  if(hasName && fileLength) {
    if(fileLength === 1)  this[name] = files[0]
    else this[name] = files // multipe: files is array,
  }
  else console.log('no file')
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank bro. Can you tell me more.. if in the three input files there is a multipe image upload input, what should I do?
I have re-edited the question. there is a multiple input file. When I select three input images that all have images, I also want them to take the same methods. Please help me again.Thank you
Look at my answer, I have handled the problem of multiple image, When multiple files, multiple files are assigned. I’ve re-edited the answers, and the one below should answer your question. My English is not good.
1

you can try this as well-

<template>
  <div class="container">
    <div class="row">
      <input type="file" name="file" @change="changeFile" /><br />

      <input type="file" name="logo" @change="changeFile" /><br />

      <input type="file" name="headerImg" multiple @change="changeFile" /><br />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      file: null,
      logo: null,
      headerImg: [],
    };
  },
  methods: {
    changeFile(event) {
      try {
        let files = event.target.files;
        if (event.target.name === "headerImg") {
          for (const property in files) {
            // there are many properties in files array
            // we will take only array values properties
            // in which uploaded file exists. so check that property
            // should be a valid number, i.e 0, 1,2
            if (!isNaN(property)) {
              this.headerImg.push(event.target.files[property]);
            }
          }
        } else {
          this[event.target.name] = files[0];
        }
      } catch (error) {
        console.error(error);
      }
    },
  },
};
</script>

2 Comments

can you specify the inside of the for loop for me? Thanks
Yeah sure! The "event.target.files" is an object with uploaded files, so when we loop in files, every file has 2 extra properties- "length", and a "function's native code". We need to ignore these two properties and only take index properties like 0, 1 so that we can use them in the loop like this "event.target.files[index]". (index could be 0, 1,2 etc.) So to take only number type properties, we have used the "isNan" function that will ignore "Not a number" type properties like "length", "function" etc.

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.