8

I'm using $.ajax,i can do that:

var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt){
  if (evt.lengthComputable) {
    var percentComplete = evt.loaded / evt.total;
    //Do something with upload progress
    console.log(percentComplete);
  }
}, false);

But,when i use fetch api,what should i do?

var fData = new FormData();
fData.append("userfile",document.getElementById("file").files[0]);
fetch("/", {method:"POST",body:fData}).then(function(res){});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
huiting Chen
  • 187
  • 2
  • 8

1 Answers1

6

As of right now, you can't. It's not part of the spec yet. There was an issue for it on the spec's repo but it's closed. There's also some discussion on this issue about it.

Long story short, you are probably better off just sticking with xhr for now unless you want to try some of the polyfills/workarounds in the second issue Iinked.

It's just not supported, and that is also the reason fetch requests don't show up in the network tab in the devtools untill they've completed.

ivarni
  • 17,658
  • 17
  • 76
  • 92