Skip to content
Advertisement

How can I get the onDownloadProgress percentage with axios get method?

Here is my code :

React.useEffect(() => {
        axios
            .get('https://jsonplaceholder.typicode.com/photos', {
                onDownloadProgress: (progressEvent) => {
                    let percentCompleted = Math.round(progressEvent.loaded * 100 / progressEvent.total);
                    console.log(progressEvent.lengthComputable);
                    console.log(percentCompleted);
                }
            })
            .then((response) => {
                console.log('response done');
            })
            .catch((error) => {
                console.log(error.message);
            });
    }, []);

And this is the console.log :

false
Infinity
false
Infinity
response done

I get infinity while trying to get the get method progress event . How can I get the exact percentage for the axios get method ?

Advertisement

Answer

I solved the issue by using another api for get request . Simply the jsonplaceholder api doesn’t have progress value for the get request .

axios.get('https://systran-systran-platform-for-language-processing-v1.p.rapidapi.com/translation/text/translate',
 { onDownloadProgress: (progressEvent) => {

     let percentCompleted = Math.round(progressEvent.loaded * 100 / 
     progressEvent.total);
     console.log(progressEvent.lengthComputable);
     console.log(percentCompleted);

  }
})
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement