Home > Blockchain >  VUE.js Refreshing img scr after submitting a form
VUE.js Refreshing img scr after submitting a form

Time:03-31

I'm facing the following issue with vue.js2.
After submitting a form, I want to refresh image src.
When the form is submitted, a new image with a chart is generated in the back-end.
This new chart image replaces the old one, however, the URL stays the same.
To update image, I'm using v-bind on image src and bind it to one of the data variables.
The starting image which is displayed before submitting the form is placeholder.jpg.
After receiving a response, I call changeChart method to update it with graph.jpg.
This action works and image is updated

The problem I'm facing here is when I update one of the values and click submit again, the image does not change.
However, when I click clear method first and set chart_url to placeholder.jpg again on next submit, image changes properly.

<v-img 
    v-bind:src=this.chart_url 
</v-img>


<script>
export default {
    name: "CenterComponent",
      data: function () {
        return {
            value1: "",
            value2: "",
            value3: "",
            output: null,
            chart_url: "http://127.0.0.1:5000/media/pictures/placeholder.jpg",
    }
},

methods:{
    clear(){
        this.value1 = "";
        this.value2 = "";
        this.value3 = "";
        this.output = "";
        this.chart_url = "http://127.0.0.1:5000/media/pictures/placeholder.jpg";
    },
    changeChart(){
        this.chart_url = "http://127.0.0.1:5000/media/pictures/graph.jpg"
    },
    submitForm(){
        fetch("http://127.0.0.1:5000/predict",{
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                value1: this.value1,
                value2: this.value2,
                value3: this.value3
            })
        }
        ).then(response => {
            if (response.ok) {
                return response.json();
            }
        }).then(data => {
            this.output = data["prediction"];
            this.changeChart();
        });
    }
},
};
</script>

CodePudding user response:

As you say, the first time you're submitting the form you actually change the url from 'placeholder.jpg' to 'graph.jpg', and on subsequent submits it remains as 'graph.jpg'.

This means that the browser is not going to fetch that image again every time it changes to 'graph.jpg' as it is cached. To make the browser fetch a new image you can try cache-busting by appending a random number to the end of the url in your changeChart method. You're effectively telling the browser that it doesn't have this image, so go fetch it.

changeChart(){
    const randomNumber = Math.floor(Math.random() * 1000)
    this.chart_url = `http://127.0.0.1:5000/media/pictures/graph.jpg?cachebust=${randomNumber}`
},

This will append a random number between 1-1000, but you could obviously improve this depending on your use case.

Alternatively, create a unique image name on the server when it is processed, and return this new name in the response of the POST call. That way you won't have this issue to start with.

  • Related