2

I'm using XMLHttpRequest to upload .psd files in my application & listening to events with addEventListener.

Code sample:

const request = new XMLHttpRequest();

request.open('POST', url);
request.upload.addEventListener('progress', ({ lengthComputable, loaded, total }) => {
  if (lengthComputable) {
    const feedData = { loaded, total };
    // do something with the data
  }
});

How can I use fetch in place of XMLHttpRequest & access the lengthComputable, loaded & total?

Because I want to show a loading bar indicating the uploading progress with that data.

GreyArk
  • 67
  • 1
  • 8
  • 1
    So you want a progress bar for the upload? If so then I have submitted a answer for this [**My Answer**](https://stackoverflow.com/questions/26674575/php-upload-extract-and-progressbar/26679480#26679480) I think the `addEventListener()` you want to set is `progress` for your `request` – NewToJS Oct 13 '18 at 03:37
  • 1
    https://stackoverflow.com/questions/52422662/how-to-get-file-upload-progress-with-fetch-and-whatwg-streams asks basically the same question but also hasn’t received any answers yet. The gist of it is that for uploads with the fetch API, browsers unfortunately don’t yet support any way to expose the progress information you’d need. So I guess this is one of the few cases where you’d might want to still use XHR instead of the fetch API – sideshowbarker Oct 13 '18 at 03:39

0 Answers0