1

I have a problem with a file reader, it load the file never the fist time, and it's like a queue. I mean: it start from the second time I select a file but it upload the first.

Can I solve this?

Here the code.

        <v-text-field
          v-if="switch1"
          label="Upload Fattura"
          @click='onPickFile'
          v-model='fatturaFileName'
          prepend-icon="mdi-paperclip"
        ></v-text-field>
        <input
          type="file"
          style="display: none"
          ref="fileInput"
          accept="text/xml"
          @change="onFilePicked"
        >


  onPickFile () {
    this.$refs.fileInput.click();
     }`

 onFilePicked (event) {
      if (event) event.preventDefault();
      var files = event.target.files[0];
      if (files !== undefined) {
        this.fatturaFileName = files.name;

        // If valid, continue
        const fr = new FileReader();
        fr.readAsText(files);
        fr.addEventListener('load', () => {
          this.fatturaUpload = fr.result;
        });
      } else {
         this.fatturaFileName = ''
         this.fatturaFileObject = null
      }
      console.log(this.fatturaUpload);
  }
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
cenzowm
  • 23
  • 6

1 Answers1

1

It works fine using the following code :

onFilePicked (event) {
      if (event) event.preventDefault();
      var fichier = event.target.files[0];
      if (fichier != undefined) {
           const fr = new FileReader();
        fr.readAsDataURL(fichier);
        var reader = new FileReader();
        let file = fichier;
        reader.readAsText(file, "UTF-8");
        reader.onload = evt => {
            console.log("1",evt.target.result)
        };
      } else {
        console.log("2",fichier)
         this.fatturaFileName = ''
         this.fatturaFileObject = null
      }
      console.log("3",this.fatturaUpload);
       this.fatturaFileName = '';
       this.fatturaFileObject = null;

    }


See the Pen Vuetify Template by boussadjra (@boussadjra) on CodePen.
Community
  • 1
  • 1
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164