0

I have some problems with uploading multiple files in Vue with Axios. The problem is that the FileList always returns a length of 0, even if I select multiple files.

I have a button which opens a File Upload:

<v-icon @click="attachFile(user)">fas fa-paperclip</v-icon>

The function it calls:

attachFile(user) {
  document.getElementById(user.id).click();
},

The form input it refers to:

 <input
    :id="item.id"
    name="files[]"
    type="file"
    multiple
    hidden
    ref="files"
    @change="handleUpload()"
 />

And then in the handleUpload() I want to make an axios call to upload the files.

handleUpload() {
  this.files = this.$refs.files.files;
  console.log(this.files);
},

But this.files always returns a FileList with the length of 0. I already set the file data in the function:

 data: () => ({
    files: "",
  })

2 Answers 2

4

As far as I can see your attempt seems to be fine basically. The error might be in your $ref itself. Instead of using a reference, you might solve your problem by simply accessing the passed in event in your function handleUpload(). You could achieve this by something like this.

handleUpload(e) {
  console.log(e.target.files);
}

This should probably work. A working example could be found here.

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

Comments

1

Maybe this will help someone(not for the author, because I see that he avoided my mistake). I used an event 'click', but have to use a 'change' event. This thing has stoled a couple of hours from me.

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.