V
V
Vitaly2019-03-20 15:16:58
JavaScript
Vitaly, 2019-03-20 15:16:58

How to access file upload status in Vue.js + axios, from another component?

I have a service component for working with api (ApiManager.js) , it has a method for uploading files:

uploadImage(imagFormData) {
    return axios.post('https://apiurl/image', imagFormData, {
      headers: {
        "Authorization": "Client-ID"
      },
      onUploadProgress: uploadedEvent => {
        console.log('Upload proccess -> ', Math.round(uploadedEvent.loaded / uploadedEvent.total * 100) , '%' )
      }
    })
      .then(response => {
        console.log('uploaded resp ---> ', response);
      })
  }


And there is an UploadImageComponent.vue component in which this method is called:
import ApiManager from '@/services/ApiManager'

ApiManager.uploadImage(imagFormData).then(response =>{
   ...
})


With this architecture in the UploadImageComponent.vue component, how can I access the upload status?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alex, 2019-03-20
@Scorpiored88

onUploadProgress({loaded, total}) {
  const uploadProgress = Math.round(loaded / total * 100)
  
  store.commit('uploader/setProgress', uploadProgress)
}

export const uploader = {
  namespaced: true,
  state: {
    progress: 0
  },
  mutations: {
    setProgress(state, progress) {
      state.progress = progress
    }
  }
}

computed: {
  ...mapState('uploader', [
    'progress'
  ])
}

<template>
  <div>{{progress}}%</div>
</template>

N
Nikita Dergachov, 2019-03-20
@vanillathunder

You can throw an event and handle it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question