How to get File upload progress with fetch() and WhatWG streams

后端 未结 2 1036
粉色の甜心
粉色の甜心 2021-02-19 09:17

Note: I\'m not looking for any alternatives. I know this can be done with XMLHttpRequest. I also don\'t care about browser support. I just want learn about the new/upcoming

2条回答
  •  自闭症患者
    2021-02-19 09:44

    As Kyle said, ReadableStream uploading is not supported yet. https://github.com/whatwg/fetch/issues/95

    Even if it where possible I would not try to monitor the upload progress throught streams, (that is if FetchObserver becomes a thing) Nobody is working on it right now. But Mozilla made a proposal that looks something like this.

    /*
    enum FetchState {
      // Pending states
      "requesting", "responding",
    
      // Final states
      "aborted", "errored", "complete"
    };
    */
    
    fetch(url, {
      observe(observer) { 
        observer.onresponseprogress = e => console.log(e);
        observer.onrequestprogress = e => console.log(e);
        observer.onstatechange = n => console.log(observer.state)
      }
    )
    

    I remember that i tested it using some experimental flags a long time ago but can't find the demo anymore, guess they removed it from MDN since it was there own implementation/suggestion.

提交回复
热议问题