6

I have a file input and I need to clear it after file is uploaded. I tried setting null value to v-model, but it generated the following error

File inputs are read only. Use a v-on:change listener instead.

My code is

<input id="fileupload" type="file" v-model="file" multiple v-on:change="uploadFile" ref="fileInput" />

How can I clear the file input in vue.js after upload so that I can upload the same file multiple times continuously

LJP
  • 1,811
  • 4
  • 23
  • 34
  • Possible duplicate of [How can I clear an HTML file input with JavaScript?](https://stackoverflow.com/questions/1703228/how-can-i-clear-an-html-file-input-with-javascript) – str Jun 05 '17 at 09:19
  • Try this document.getElementById("fileupload").value = ""; – Lalit Jun 05 '17 at 09:21
  • Can we use v-model to do the same? – LJP Jun 05 '17 at 09:24

3 Answers3

2

You are using v-on:change="uploadFile" and guessing that your uploadFile has a success callback.

You can do the following:

  • Wrap your input in a form and add a ref attribute to your form:

    <form ref="myFileInputForm">
          <input id="fileupload" type="file" v-model="file" multiple v-on:change="uploadFile" ref="fileInput" />
     </form>
    
  • In your successful callback uploadFile do this:

    this.$refs.myFileInputForm.reset();

James Martinez
  • 191
  • 2
  • 11
Vamsi Krishna
  • 30,568
  • 8
  • 70
  • 78
2

Solved it using the ref attribute,

this.$refs.fileInput.value = null; 
LJP
  • 1,811
  • 4
  • 23
  • 34
0

File inputs are read only. don't bind on element, remove v-model="file"

Ixoy Well
  • 17
  • 1