Axios onUploadProgress only fires once on my machine
Asked Answered
G

2

9

If I use this fiddle https://jsfiddle.net/v70kou59/1/ everything works as expected

(function () {
    var output = document.getElementById('output');
    document.getElementById('upload').onclick = function () {
      var data = new FormData();
      data.append('foo', 'bar');
      data.append('file', document.getElementById('file').files[0]);
      var config = {
        onUploadProgress: function(progressEvent) {
          var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
          console.log(percentCompleted)
        }
      };
      axios.put('/upload/server', data, config)
        .then(function (res) {
          output.className = 'container';
          output.innerHTML = res.data;
        })
        .catch(function (err) {
          output.className = 'container text-danger';
          output.innerHTML = err.message;
        });
    };
  })();

But if I download the axios examples repo and install the necessary dependencies the callback function, onUploadProgress no longer works as expected. It only fires onUploadProgress once with "100". https://github.com/axios/axios/tree/master/examples

Could this be my version of node? It seems like it must be my machine.

Gorden answered 16/10, 2017 at 22:48 Comment(2)
Did you try with a big enough file? Maybe the data doesn't get chunked the same way locally and instantly completes the upload?Noncontributory
Yeah I believe this was the issue. Jpegs are too small and it was completing instantly. My confusion comes from that jfiddle example because in that example the progress is gradual and not instant even with jpegs.Gorden
I
10

I've had exact issue. One of the reason(it was the case with me) could be, your network is too fast to trigger the progressEvent multiple times.

To simulate this, slow down your network on Network tab of your Browser.

Slow 3G setting worked for me on Google Chrome/New Edge(dev release) browser.

Refer the image:

enter image description here

PS: I know this is too late to answer this. But others might find it helpful when they land here.

Itinerary answered 22/1, 2020 at 7:21 Comment(1)
is there a way to enforce the split even on speed netwrok?Drawing
S
-3

Because the new version changed.

enter image description here

So you have to call progress instead of onUploadProgress

 static uploadImageFile(authId, userinfo, file, callback) {
        let url = AppConfig.domain + '/api/CoreUploads/uploadImg';
        let formData = new FormData();
        formData.append('realm', userinfo.realm);
        formData.append('fileurl', `users/${userinfo.id}/`);
        formData.append('cropData', "");
        formData.append('autoid', true);
        formData.append('image', file);

        return axios.post(url, formData, {
            timeout: 1000000,
            withCredentials: true,
            headers: {
                'Authorization': authId,
                'Content-type': 'multipart/form-data'
            },
            progress: function (progressEvent) {
                let percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
                callback(percentCompleted);
            }
        }).catch(e => {
            console.log(e);

        });
    }
Shanika answered 4/4, 2018 at 9:39 Comment(5)
There is no method called progress: () => {}Widmer
see my screenshot above, that shows you the source code of progress methodShanika
onUploadProgress: (progressEvent) => {} is what works.Deckard
I tested this and is not working, just use onUploadProgressSassaby
In case anyone is confused by this, the explanation is that the old way of doing it was via the progress method. Now it's onUploadProgress. Most people will want the latter unless they are using a very, very old version of axios. See https://mcmap.net/q/247528/-axios-onuploadprogress-and-ondownloadprogress-not-working-with-cors for more details.Catamaran

© 2022 - 2025 — McMap. All rights reserved.