0

Assign filename to input field on upload and then show success message working fine,but when i try to delete file and upload again it's not working!

new Vue({
  el: "#app",
  data() {
    return {
      form: {
        message: '',
        fileurl: ''
      },
      loading: false
    }
  },
  methods: {


    uploadImage(event) {

      this.form.fileurl = 'uploaded!'
    },
    deleteFile(furl) {

      this.form.fileurl = ''
    }

  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

span {
  color: red;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>


<div id="app">
  <h3>
    File Upload / remove Demo
  </h3>
  <hr />

  <div class="custom-file attach_file" v-show="!form.fileurl">

    <input type="file" id="file" name="file" @change="uploadImage($event)">

    <input type="text" v-model="form.fileurl">

  </div>

  <p v-if="form.fileurl"> {{ form.fileurl }} <span @click="deleteFile(form.fileurl)">Delete</span></p>

</div>

I am not getting any console error as well.

This is what i have tried so far. Can you guys please have a look at this!

0

2 Answers 2

1

The problem is that @change is not trigger when you choose the same file. The simplest solution is reset value of input when you click delete

this.$refs.fileToUpload.value = '';

new Vue({
  el: "#app",
  data() {
    return {
      form: {
        message: '',
        fileurl: ''
      },
      loading: false
    }
  },
  methods: {


    uploadImage(event) {

      this.form.fileurl = 'uploaded!'
    },
    deleteFile(furl) {

      this.form.fileurl = ''
      this.$refs.fileToUpload.value = '';
    }

  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

span {
  color: red;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>


<div id="app">
  <h3>
    File Upload / remove Demo
  </h3>
  <hr />

  <div class="custom-file attach_file" v-show="!form.fileurl">

        <input type="file" id="file" name="file"  class="custom-file-input" @change="uploadImage($event)" ref="fileToUpload">

    <input type="text" v-model="form.fileurl">

  </div>

  <p v-if="form.fileurl"> {{ form.fileurl }} <span @click="deleteFile(form.fileurl)">Delete</span></p>

</div>

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

Comments

0

You need to clear you input field first.

Update your upload method with this.

 uploadImage(event) {
     this.form.fileurl =  'uploaded!'
     this.success = false
     this.$nextTick(() => {
     this.success = true
 })

Have a look at this working demo

https://jsfiddle.net/asimshahzad/8c9pcfys/

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.