How can I use deflated/gzipped content with an XHR onProgress function?

前端 未结 8 2364
醉酒成梦
醉酒成梦 2020-12-08 00:11

I\'ve seen a bunch of similar questions to this get asked before, but I haven\'t found one that describes my current problem exactly, so here goes:

I have a page whi

8条回答
  •  离开以前
    2020-12-08 01:15

    A slightly more elegant variation on your solution would be to set a header like 'x-decompressed-content-length' or whatever in your HTTP response with the full decompressed value of the content in bytes and read it off the xhr object in your onProgress handler.

    Your code might look something like:

    request.onProgress = function (e) {
      var contentLength;
      if (e.lengthComputable) {
        contentLength = e.total;
      } else {
        contentLength = parseInt(e.target.getResponseHeader('x-decompressed-content-length'), 10);
      }
      progressIndicator.update(e.loaded / contentLength);
    };
    

提交回复
热议问题